Sample code:
Copy Code code as follows:
BULKSTOCKBLL BLL = new BULKSTOCKBLL ();
DataSet ds = BLL. Getbulkstock ();
This.ddl_BulkStock.DataTextField = "Name";
This.ddl_BulkStock.DataValueField = "ID";
This.ddl_BulkStock.DataSource = ds;
This.ddl_BulkStock.DataBind ();
THIS.DDL_BULKSTOCK.ITEMS.ADD (New ListItem ("All", "0"));//First choice!
This.ddl_BulkStock.Items.FindByValue ("0"). Selected = true;
*******************************
This.ddl_BulkStock.Items.FindByValue (Infobulkstockid). Selected = true;//second choice!
******************************
There were two choices, so the error occurred.
Page load, the index0 selected = True, you choose a index1 selected also true, on the error.
Don't use dropdownlist.items[x]. Selected = True/false, recommended with Dropdownlist.selectedindex = x.
There are two solutions:
(1) When selecting an option, use the following code without using the green code shown above
this. Ddl_bulkstock.selectedindex = Ddl_BulkStock.Items.IndexOf (Ddl_BulkStock.Items.FindByValue (Infobulkstockid)) ;
(2) Use the ClearSelection action before selecting an option each time.
This.ddl_BulkStock.ClearSelection ();
Today on the edit page: You cannot select multiple items in DropDownList.
Careful to find the cause of the discovery:
Copy Code code as follows:
<asp:dropdownlist id= "Com_ygrenshu" runat= "Server" style= "MARGIN-LEFT:8PX;" >
<asp:listitem value= "0" > Please select number </asp:ListItem>
<asp:listitem selected= "True" value= "5" >5 person below </asp:ListItem>
<asp:listitem value= "Ten" >5-10 people </asp:ListItem>
<asp:listitem value= ">11-50" person </asp:ListItem>
</asp:DropDownList>
I am editing the page to rebind the data code:
Copy Code code as follows:
for (int i = 0; i < This.Com_Ygrenshu.Items.Count; i++)
{
Com_ygrenshu.items[i]. Selected = false;
Without the above sentence, there will be an error: You cannot select multiple items in DropDownList
if (com_ygrenshu.items[i). Value.trim () = ds. Tables[0]. rows[0]["Empl_num"]. ToString (). Trim ())
{
Com_ygrenshu.items[i]. Selected = true;
}
}
The reason: Your DropDownList has two selected= "True" option, so make an error!
#dropdownlist控件