码迷,mamicode.com
首页 > Web开发 > 详细

webform基本控件-----DropDownList

时间:2015-08-10 17:55:51      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

把内容填进去:

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;
            }
        }

 

webform基本控件-----DropDownList

标签:

原文地址:http://www.cnblogs.com/qianxiaojinnian/p/4718336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!