Problem with DropDownList getting SelectIndex to be 0 all the time

  • 2020-12-19 20:58:29
  • OfStack

1. DropDownList ="true" property must be set if you want DropDownList to commit automatically. Here is the code:
 
<asp:DropDownList ID="ddlNameList" runat="Server" Height="30" 
AutoPostBack="True" onselectedindexchanged="ddlNameList_SelectedIndexChanged" ></asp:DropDownList> 

2. When processing on the server side, especially when initializing DropDownList, I didn't notice that the result was wrong. The following is the error code:
 
protected void Page_Load(object sender, EventArgs e) 
{ 

if (!Page.IsCallBack) 
{ 
this.fillIntoNameList(); 
} 
} 

This initialization judgment is wrong, and it is initialized once each time it is sent to the server, which results in getting SelectIndex for DropDownList only being 0 each time

Correct code, as follows:
 
protected void Page_Load(object sender, EventArgs e) 
{ 

if (!Page.IsPostBack) 
{ 
this.fillIntoNameList(); 
} 
} 

Related articles: