ASP. Four Methods of Binding Data for DropDownList Drop down List Control in NET

  • 2021-07-13 05:05:40
  • OfStack

The DropDownList Web server control enables the user to select 1 item from a predefined list. It differs from the ListBox Web server control in that its list of items is hidden until the user clicks the drop-down button. In addition, the DropDownList control differs from the ListBox control in that it does not support multiple selection modes.

The presentation of DropDownList in html corresponds to select. Let's look at several methods of binding data in DropDownList.

1. Tie an Array array to an DropDownList


string[] Month =new string[7]{ "January", "February", "March", "April", "May", "June", "July" };
this.DropDownList1.DataSource = Month;
this.DropDownList1.DataBind();

This method can only bind one set of data to DropDownList, because DropDownList can bind two kinds of data: 1 is DataTextField and 2 is DataValueField, so the value of DataTextField after binding by the first method = = DataTextField value.

2. Bind dynamic Array arrays to DropDownList


ArrayList ar = new ArrayList();
for (int i = 1; i <=12; i++)
{
    ar.Add(i+" Month ");
}
this.DropDownList2.DataSource = ar;
this.DropDownList2.DataBind();

Essentially, it is said that January to December are added to the array, as follows:


ArrayList ar = new ArrayList();
ar.Add("1 Month ");
ar.Add("2 Month ");
ar.Add("3 Month ");
ar.Add("4 Month ");
...
this.DropDownList2.DataSource = ar;
this.DropDownList2.DataBind();

The advantage of this method is that the function of dynamically adding elements can be realized through ArrayList. Add. For example, if there is an DataTable, we should read out one row of data in DataTable and add it to Arraylist.

Look at the code shown below


ArrayList ar = new ArrayList();
DataTable dt=dataset.Tables[0]
foreach (DataRow dr in dt.Rows)
{
    ar.Add(dr[0].ToString());
}

The above code from 1 DataTable through the foreach statement loop read Table 1 row data in the first grid value added to the ArrayList.

3. The advantage of binding Hashtable to Hashtable in Dropdownlist is that it can also bind two kinds of data: one is "key and the other is" value ", so that we can bind two different kinds of data for dropdonwlist.


Hashtable Ht = new Hashtable();
Ht.Add("January", "1 Month ");
Ht.Add("February", "2 Month ");
Ht.Add("March", "3 Month ");
Ht.Add("April", "4 Month ");
Ht.Add("May", "5 Month ");
Ht.Add("June", "6 Month ");
Ht.Add("July", "7 Month ");
this.DropDownList3.DataSource = Ht;
this.DropDownList3.DataValueField = "key";
this.DropDownList3.DataTextField = "value";
this.DropDownList3.DataBind();

4. Bind an Object object to an dropdownlist

First, add a new class with the following structure


public class ClassMonth
{
    private string _MonthEN = DateTime.Now.ToString("MMMM",System.Globalization.CultureInfo.CreateSpecificCulture("en"));
    private string _MonthCN = DateTime.Now.ToString("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN"));
    public ClassMonth()
    {
        MonthCN = DateTime.Now.ToString("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN"));
        MonthEN = DateTime.Now.ToString("MMMM", System.Globalization.CultureInfo.CreateSpecificCulture("en"));
    }
    public ClassMonth(string cn,string en)
    {
        MonthCN = cn;// Import variables to assign values to attributes
        MonthEN = en;// Import variables to assign values to attributes
       
    }
    public string MonthEN // Construct attribute
    {
       get
        {
            return _MonthEN;
        }
        set
        {
            _MonthEN = value;
        }
    }
    public string MonthCN  // Construct attribute
    {
        get
        {
            return _MonthCN;
        }
        set
        {
            _MonthCN = value;
        }
    }
}

Binding method


ArrayList arlist=new ArrayList();
arlist.Add(new ClassMonth("1 Month ", "January"));
arlist.Add(new ClassMonth("2 Month ", "February"));
arlist.Add(new ClassMonth("3 Month ", "March"));
arlist.Add(new ClassMonth("4 Month ", "April"));
arlist.Add(new ClassMonth("5 Month ", "May"));
this.DropDownList4.DataSource = arlist;
this.DropDownList4.DataValueField = "MonthEN";
this.DropDownList4.DataTextField = "MonthCN";
this.DropDownList4.DataBind();


Related articles: