把内容填进去:
private void FillNation() { DropDownList1.Items.Clear(); //查数据 List<Nation> list = _Context.Nation.ToList(); //送进去---循环 ListItem temp = new ListItem("==请选择==", "-1"); DropDownList1.Items.Add(temp); foreach (Nation data in list) { ListItem li = new ListItem(data.Name, data.Code); DropDownList1.Items.Add(li); } //送进去---绑定 DropDownList1.DataSource = list; DropDownList1.DataTextField = "Name"; DropDownList1.DataValueField = "Code"; DropDownList1.DataBind(); // ListItem temp = new ListItem("==请选择==", "-1"); DropDownList1.Items.Insert(0, temp); }
把值取出来:
//取值---SelectedValue Label1.Text = DropDownList1.SelectedValue;
//取值---SelectedItem Label1.Text = DropDownList1.SelectedItem.Text + DropDownList1.SelectedItem.Value;
//取值---SelectedIndex Label1.Text = DropDownList1.Items[DropDownList1.SelectedIndex].Value;
//取值---遍历 foreach (ListItem li in DropDownList1.Items) { if (li.Selected == true) { Label1.Text = li.Text + li.Value; } }
设定某项为选中值:
//SelectedValue DropDownList1.SelectedValue = TextBox1.Text;
//SelectedIndex DropDownList1.SelectedIndex = Convert.ToInt32(TextBox1.Text);
//遍历 DropDownList1.SelectedIndex = -1; foreach (ListItem li in DropDownList1.Items) { if (li.Value == TextBox1.Text) { li.Selected = true; } }