ASP.NET 绑定的下拉框等方法(RadioButtonList1,DropDownList1...等单选列表,下拉列表同理调整属性即可)

一.效果很简单几个属性与Winfrom 几乎相同

Winfrom:

cobbox.DisplayMembe=“显示值”
cobbox.ValueMember=”绑定值ID”

1.死绑:Winfrom几个属性一个一个添加

public void BindCob()
        {
            cmbSex.Items.Add("---请选择----");
            cmbSex.Items.Add("");
            cmbSex.Items.Add("");
            cmbSex.Text = "---请选择----";
        }

2.对象构造器集合一个一个添加

 public void cmbRolebind()
        {
            cmbRole.DisplayMember = "U_RealName";
            cmbRole.ValueMember = "U_Role";
            List Roles = new List();
            Roles.Add(new UserModel() { S_ID = 2, U_LoginName = "A2", U_Password = "123456", U_RealName = "管理员", U_Sex = "", U_Telephone = "1234566666", U_Role = 1, U_CanDelete = false });
            Roles.Add(new UserModel() { S_ID = 2, U_LoginName = "A2", U_Password = "123456", U_RealName = "店长", U_Sex = "", U_Telephone = "1234566666", U_Role = 2, U_CanDelete = false });
            Roles.Add(new UserModel() { S_ID = 2, U_LoginName = "A2", U_Password = "123456", U_RealName = "营业员", U_Sex = "", U_Telephone = "1234566666", U_Role = 3, U_CanDelete = false });
            //foreach (var Role in Roles)
            //{
            //    cmbRole.Items.Add(Role);
            //}
            cmbRole.DataSource = Roles;
            cmbRole.SelectedIndex = 0;
        }

3.活的数据库;上面是一个数据库的方法

 public void binlist()
        {
            string sql = "select * from Users";
            DataTable dt = db.selectdt(sql, null, false);
            combox.DataSource = dt;
            combox.DisplayMember = "U_RealName";
            combox.ValueMember = "U_ID";
        }    
winfrom

 

二,1.ASP.NET类似:死绑

  public void bindrdolist()
        {
            List m1 = new List()
            {
               new Mylist(){id=1,name="2"},
               new Mylist(){id=3,name="3"},
               new Mylist(){id=4,name="4"}
            };
            RadioButtonList1.DataSource = m1;
            RadioButtonList1.DataTextField = "name";
            RadioButtonList1.DataValueField = "id";
            RadioButtonList1.DataBind();
        }

2.活绑

  public void binlist()
        {
            string sql = "select * from Users";
            DataTable dt = db.selectdt(sql, null, false);
            DropDownList1.DataSource = dt;
            DropDownList1.DataTextField = "U_RealName";
            DropDownList1.DataTextField = "U_ID";
            DropDownList1.DataBind();
        }      

 

你可能感兴趣的:(ASP.NET 绑定的下拉框等方法(RadioButtonList1,DropDownList1...等单选列表,下拉列表同理调整属性即可))