"ASP." DropDownList data binding a small bug

Source: Internet
Author: User

Binding data in this case, obviously in the first item inserted a data, but always in the DropDownList is not displayed.

The code is as follows:

if(!IsPostBack) {                //Bound CityDataTable dtcity =NewHighsearch ().                Getcityselect (); Dropdownlist1.datasource=dtcity; Dropdownlist1.datavaluefield="Citycode"; Dropdownlist1.datatextfield="CityName";                DataBind (); DropDownList1.Items.Insert (0,NewListItem ("Please select a city","")); DropDownList2.Items.Insert (0,NewListItem ("Please select line","")); DropDownList3.Items.Insert (0,NewListItem ("Please select a site","")); //Binding ProjectDataTable Dtproject =NewHighsearch ().                Getprojectselect (); Dropdownlist4.datasource=Dtproject; Dropdownlist4.datavaluefield="ProjectName"; Dropdownlist4.datatextfield="ProjectName";                DataBind (); DropDownList4.Items.Insert (0,NewListItem ("Please select a project","")); }

The results are as follows:

It took a long time to find the reason because DropDownList had to bind the data before inserting the option.

Modify the code as follows:

if(!IsPostBack) {                            //Binding ProjectDataTable Dtproject =NewHighsearch ().                Getprojectselect (); Dropdownlist4.datasource=Dtproject; Dropdownlist4.datavaluefield="ProjectName"; Dropdownlist4.datatextfield="ProjectName";                         DataBind (); //Bound CityDataTable dtcity =NewHighsearch ().                Getcityselect (); Dropdownlist1.datasource=dtcity; Dropdownlist1.datavaluefield="Citycode"; Dropdownlist1.datatextfield="CityName";                DataBind (); DropDownList1.Items.Insert (0,NewListItem ("Please select a city","")); DropDownList2.Items.Insert (0,NewListItem ("Please select line","")); DropDownList3.Items.Insert (0,NewListItem ("Please select a site","")); DropDownList4.Items.Insert (0,NewListItem ("Please select a project","")); }        

This bug was resolved.

  It is important to remember that when binding multiple data, you must first bind all the data and then insert the first item of data.

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.