How do I add a default "Select" option to this ASP.NET DropDownList control?

I am a new ASP.NET developer and I am trying to learn Linq-To-Entities. I am trying to associate a DropDownList with a Linq statement to get a status list in a status object. Everything is working fine. However, I am currently trying to add the “Select” option to DropDownList, but it doesn’t work with me. Could you tell me how to fix this?

ASP.NET Code:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 

Code-Behind:

 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DropDownList1.Items.Add(new ListItem("Select", "0", true)); bindStatusDropDownList(); } } private void bindStatusDropDownList() { Status status = new Status(); DropDownList1.DataSource = status.getData(); DropDownList1.DataValueField = "ID"; DropDownList1.DataTextField = "Description"; DropDownList1.DataBind(); } 

UPDATE:

I also tried to make DropDownList in the markup set, but it did not work with me.

 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem Selected="True" Value="0" Text="Select"></asp:ListItem> </asp:DropDownList> 
+9
c # linq-to-entities
Dec 20 '13 at 9:37
source share
6 answers

The reason it doesn't work is because you add an item to the list, and then redefine the entire list with a new DataSource , which clears and refills your list, losing the first manually added item.

So you need to do this in reverse order:

 Status status = new Status(); DropDownList1.DataSource = status.getData(); DropDownList1.DataValueField = "ID"; DropDownList1.DataTextField = "Description"; DropDownList1.DataBind(); // Then add your first item DropDownList1.Items.Insert(0, "Select"); 
+22
Dec 20 '13 at 9:40
source share

Although this is a fairly old question, another approach is to change the AppendDataBoundItems property. Thus, the code will be:

 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AppendDataBoundItems="True"> <asp:ListItem Selected="True" Value="0" Text="Select"></asp:ListItem> </asp:DropDownList> 
+9
Oct. 15 '15 at 9:37
source share

I tried using the following code. it works fine for me

 ManageOrder Order = new ManageOrder(); Organization.DataSource = Order.getAllOrganization(Session["userID"].ToString()); Organization.DataValueField = "OrganisationID"; Organization.DataTextField = "OrganisationName"; Organization.DataBind(); Organization.Items.Insert(0, new ListItem("Select Organisation", "0")); 
+2
Dec 27 '16 at 13:41
source share

Move DropDownList1.Items.Add (new ListItem ("Select", "0", "True")); After bindStatusDropDownList ();

So:

  if (!IsPostBack) { bindStatusDropDownList(); //first create structure DropDownList1.Items.Add(new ListItem("Select", "0", true)); // after add item } 
+1
Dec 20 '13 at 9:42 on
source share

If you follow “Add,” he will add it to the end of the list. You need to do "Paste" if you want the item to be added to the top of the list.

+1
Nov 12 '14 at 19:17
source share
 Private Sub YourWebPage_PreRenderComplete(sender As Object, e As EventArgs) Handles Me.PreRenderComplete If Not IsPostBack Then DropDownList1.Items.Insert(0, "Select") End If End Sub 
0
Oct. 25 '16 at 7:36
source share



All Articles