Is there an easy way to add the "-Select--" parameter to the DataBound DropDownList?

I have a DataBound DropDownList that loads fine, but I would like to add another element that says "--Select--", or something instead of automatically displaying the first DataBound element.

Is there an easy way to do this or do I need to manually add a dummy element?

Here is my code:

MyContext fc = new MyContext (); ddl.DataSource = fc.SomeTable; ddl.DataBind(); 
+4
source share
2 answers

After completing the data operation:

 ddl.Items.Insert(0, "---Select---"); 

This will add it as the first item in the list.

Alternatively, you can add a new ListItem instead of a string so that you can have the actual value instead of the string as a drop down list.

So you can do something like:

 ddl.Items.Insert(0, new ListItem("---Select---", Guid.Empty.ToString()); 
+2
source

Alternatively, add the default element to the markup and set the AppendDataBoundItems property to true.

  <asp:DropDownList ID="ddl" runat="server" AppendDataBoundItems="true"> <asp:ListItem Value="" Text="---Please Select---"></asp:ListItem> </asp:DropDownList> 
+4
source

Source: https://habr.com/ru/post/1313813/


All Articles