Cause Analysis and Solution for multiple items cannot be selected in DropDownList in asp.net

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.