ASP.NET binding methods such as drop-down box (RadioButtonList1, DropDownList1 ... and other single-selection list, the drop-down list of attributes can be adjusted the same way)

A. The effect is very simple and almost the same number of attributes Winfrom

Winfrom:

= cobbox.DisplayMembe "display value"
cobbox.ValueMember = "bound value ID"

1. dead tie: Winfrom add several attributes one by one

public void BindCob()
        {
            cmbSex.Items.Add ( " --- Select ---- " );
            cmbSex.Items.Add("");
            cmbSex.Items.Add("");
            cmbSex.Text = " --- Please select ---- " ;
        }

2. a set of object constructor added

 public void cmbRolebind()
        {
            cmbRole.DisplayMember = "U_RealName";
            cmbRole.ValueMember = "U_Role";
            List<UserModel> Roles = new List<UserModel>();
            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. live database; The above method is a database

 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

 

Two, 1.ASP.NET similar: dead tie

  public  void bindrdolist ()
        {
            List<Mylist> m1 = new List<Mylist>()
            {
               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. Live tie

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

 

Guess you like

Origin www.cnblogs.com/yijieyufu/p/12343914.html