Home>

During web development,A dropdownlist server control is used in the page,Found a very strange problem,No matter which item is selected on the page,The value obtained in the background is always the value of the first item,After looking at it for a long time, I didn't find out what the problem was.The dropdownlist control has been used countless times in development.In contrast, the other code is the same!

After a few minutes,I really ca n’t see where the problem is, so I have to find the answer online.Many people on the Internet have encountered the same problem-dropdownlist always selects the first item.The online solutions are to use if (! Ispostback) in the page_load event when the dropdownlist is bound, but I bind it like this,Still no solution found online.

Later, I thought about it quietly,Is it because when you bind the dropdownlist, you only assign the value to the text, but not the value?Then I tried to assign the value of each item, and it really did not happen!

Let's summarize two reasons why the dropdownlist control always selects the first item.

Case one, look at the following code:

Client code:

<asp:dropdownlistid="ddl1" runat="server">
</asp:dropdownlist>

Server code:

protected void page_load (object sender, eventargs e)
{
  binddropdownlist ();
}
private void binddropdownlist ()
{
  ddl1.items.clear ();//Before each binding,Clear all items first
  for (int i=1;i<= 3;i ++)
  {
    listitem item1=new listitem ();
    item1.text="Article" + i.tostring () + "item";
    item1.value="first" + i.tostring () + "item";
    ddl1.items.add (item1);
  }
}

The code example above,That is to say that the first item is always selected on the Internet (the selection cannot change the option), the binding method can be solved by writing in if (! Ispostback)code show as below:

protected void page_load (object sender, eventargs e)
{
  if (! ispostback)
  {
    binddropdownlist ();
  }
}

Case two, which I encountered,Look at the following code:

Client code:

<asp:dropdownlist runat="server">
</asp:dropdownlist>
<asp:button runat="server" text="get" onclick="btnget_click" />

Server code:

protected void page_load (object sender, eventargs e)
{
  if (! ispostback)
  {
    binddropdownlist ();
  }
}
private void binddropdownlist ()
{
  ddl1.items.clear ();//Before each binding,Clear all items first
  for (int i=1;i<= 3;i ++)
  {
    listitem item1=new listitem ();
    item1.text="Article" + i.tostring () + "item";
    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 the item1.value place,Is not assignedHowever, the result of getting the value of text is messed up,Just assign a value to value and there is no problem!

  • Previous Summary of commonly used Module installation methods in Python
  • Next jQuery implements announcement news auto scroll effect example code