Fill in the contents:
Private voidfillnation () {DropDownList1.Items.Clear (); //Check Datalist<nation> list =_context.nation.tolist (); //send it in---loop .ListItem Temp=NewListItem ("= = Please select = =","-1"); DROPDOWNLIST1.ITEMS.ADD (temp); foreach(Nation datainchlist) {ListItem Li=NewListItem (data. Name, data. Code); DropDownList1.Items.Add (LI); } //send it in---bindingsDropdownlist1.datasource=list; Dropdownlist1.datatextfield="Name"; Dropdownlist1.datavaluefield="Code"; Dropdownlist1.databind (); //ListItem temp = new ListItem ("= = Please select = =", "-1");DropDownList1.Items.Insert (0, temp); }
Take the value out:
// Value---selectedvalue Label1.Text = Dropdownlist1.selectedvalue;
// Value---selecteditem Label1.Text = DropDownList1.SelectedItem.Text + DropDownList1.SelectedItem.Value;
// Value---selectedindex Label1.Text = Dropdownlist1.items[dropdownlist1.selectedindex]. Value;
// Value---traversal foreach inch dropdownlist1.items) { iftrue) { = li. Text + li. Value; } }
Set an item to the selected value:
// SelectedValue Dropdownlist1.selectedvalue = TextBox1.Text;
// SelectedIndex
// Traverse Dropdownlist1.selectedindex =-1; foreach inch dropdownlist1.items) { if (li. Value = = TextBox1.Text ) {true; } }
WebForm Basic Control-----DropDownList