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.