How to solve the problem that DropDownList always selects the first item

  • 2021-07-02 23:57:18
  • OfStack

In the process of web page development, the use of a DropDownList server control in the page, found a very strange problem, no matter which one item is selected in the page, the value obtained in the background is always the value of the first item, looked for a long time and did not find the problem where, DropDownList control has been used countless times in the development, compared with other codes are 1!

After a few minutes, I really can't see where the problem is, so I have to look for the answer on the Internet. Indeed, many people on the Internet encounter the problem of "one kind"-DropDownList always chooses the first item. The online solution is to use if (!) for Page_Load events when binding DropDownList. IsPostBack), but I am so bound, in the network still did not find a solution.

Later, I quietly thought about it. Is it because when I bound DropDownList, I only assigned a value to Text, but not to Value? Then I tried to assign Value to every item, and there was no such phenomenon!

Now summarize the two reasons why the DropDownList control always selects item 1.

Case 1, look at the following code:
Client code:


<asp:DropDownListID="ddl1"runat="server">
</asp:DropDownList>

Server-side code:


protected void Page_Load(object sender, EventArgs e)
{
  BindDropDownList();
}
  
private void BindDropDownList()
{
  ddl1.Items.Clear(); // Clear all items before each binding 
  for (int i = 1; i <= 3; i++)
  {
    ListItem item1 = new ListItem();
    item1.Text = " No. 1 " + i.ToString() + " Items ";
    item1.Value = " No. 1 " + i.ToString() + " Items ";
    ddl1.Items.Add(item1);
  }
}

The above code case, that is to say, the first item is always selected (the option cannot be changed), and the binding method is written in if (! IsPostBack), the code is as follows:


protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    BindDropDownList();
  }
}

Case 2, which I encountered, please see the following code:
Client code:


<asp:DropDownList ID="ddl1" runat="server">
</asp:DropDownList>
&nbsp;<asp:Button ID="btnGet" runat="server" Text=" Get " onclick="btnGet_Click" />

Server-side code:


protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    BindDropDownList();
  }
}
  
private void BindDropDownList()
{
  ddl1.Items.Clear(); // Clear all items before each binding 
  for (int i = 1; i <= 3; i++)
  {
    ListItem item1 = new ListItem();
    item1.Text = " No. 1 " + i.ToString() + " Items ";
    item1.Value = "";
    ddl1.Items.Add(item1);
  }
}
  
protected void btnGet_Click(object sender, EventArgs e)
{
  string str = ddl1.SelectedItem.Text;
  Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('" + str + "');</script>");
}

Note that item1.Value is not assigned, but it leads to the confusion of obtaining the value of Text, so long as Value is assigned a value, there is no problem!

The above is about the "one kind" problem encountered by most people on the Internet-DropDownList always chooses the solution of the first item, hoping to help everyone's study.


Related articles: