asp.net can not select multiple items in the DropDownList reason analysis and solution _ practical skills

Source: Internet
Author: User
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控件
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.