Sample Code:
Copy codeThe Code is 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"); // select for the first time!
This. ddl_BulkStock.Items.FindByValue ("0"). Selected = true;
*******************************
This. ddl_BulkStock.Items.FindByValue (infobulkstockid). Selected = true; // select for the second time!
******************************
An error is returned because two selections are selected.
In page load, the value of Selected in index0 is true. If you select another index1 Selected, the system returns an error.
Do not use dropDownList. Items [x]. Selected = true/false. We recommend that you use dropDownList. SelectedIndex = x.
There are two solutions:
(1) when selecting an option, use the following code instead of the green code display method.
This. ddl_BulkStock.SelectedIndex = ddl_BulkStock.Items.IndexOf (ddl_BulkStock.Items.FindByValue (infobulkstockid ));
(2) Use the ClearSelection operation before selecting an option each time.
This. ddl_BulkStock.ClearSelection ();
The Edit page appears today: you cannot select multiple items in DropDownList.
Find out the cause carefully:
Copy codeThe Code is as follows:
<Asp: DropDownList ID = "Com_Ygrenshu" runat = "server" style = "margin-left: 8px;">
<Asp: ListItem Value = "0"> select the number of people </asp: ListItem>
<Asp: ListItem Selected = "True" Value = "5"> less than 5 </asp: ListItem>
<Asp: ListItem Value = "10"> 5-10 </asp: ListItem>
<Asp: ListItem Value = "50"> 11-50 </asp: ListItem>
</Asp: DropDownList>
Code for rebinding data on the Edit page:
Copy codeThe Code is as follows:
For (int I = 0; I <this. Com_Ygrenshu.Items.Count; I ++)
{
Com_Ygrenshu.Items [I]. Selected = false;
// If the preceding statement is not used, an error occurs: you cannot select multiple items in the 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 is: Your DropDownList has two Selected = "True" options, so an error occurred!
# Dropdownlist Control