Quote
Color Fish & software development of several binding methods DropdownList
第一种,把Array数组绑到dropdownlist
程序代码
string[] Month =new string[7]{ "January", "February", "March", "April", "May", "June", "July" };
this.DropDownList1.DataSource = Month;
this.DropDownList1.DataBind();
The first method can only be bound to one set of data DropDownList, since two data can bind drawdonwlist DataTextField 1 is
2 is the first process so DataValueField binding DataTextField value == value DataTextField
second, the Array Array dropdownlist bound to
the program code
the ArrayList new new Ar = the ArrayList ();
for (int I =. 1; I <= 12 is; I ++)
{
ar.Add (I + "month");
}
this.DropDownList2.DataSource = Ar;
the this. DropDownList2.DataBind ();
intuitive writing point.
Program code for
the ArrayList new new Ar = the ArrayList ();
ar.Add ( "Jan");
ar.Add ( "Feb");
ar.Add ( "March");
ar.Add ( "April");
..................................................
Ar = this.DropDownList2.DataSource;
this.DropDownList2.DataBind ();
Benefits second method is through ArrayList.Add way, you can add dynamic elements function, for example, there is a DataTable, we want the DataTable one line read out the data added to the Arraylist them.
Program code for
the ArrayList new new Ar = the ArrayList ();
DataTable dt = dataset.Tables [0]
the foreach (DR in the DataRow dt.Rows)
{ar.Add (DR [0] .ToString ());}
The above code from one of the DataTable foreach statement read cycle by adding the value of the first cell in the row of data in Table ArrayList to them.
The third method, the Hashtable bound to Dropdownlist among the benefits of Hashtable method is that it can also bind two kinds of data is a "key, one is the" value ", so that we can be binding on the two dropdonwlist . different types of data the
program code
the Hashtable = Ht of the new new the Hashtable ();
Ht.Add ( "January", "Jan");
Ht.Add ( "February", "February");
Ht.Add ( "March "
Ht.Add ( "April", "April");
Ht.Add ( "On May", "May");
Ht.Add ( "June", "June");
Ht.Add ( "July", "7");
this.DropDownList3.DataSource = Ht of the;
this.DropDownList3.DataValueField = "Key";
this.DropDownList3.DataTextField = "value";
this.DropDownList3.DataBind ();
fourth, the Object object tie dropdownlist given to
first add a class, the following structure
program code
public class ClassMonth
{
Private String _MonthEN = DateTime.Now.ToString ( "MMMM", System.Globalization.CultureInfo.CreateSpecificCulture ( "EN"));
Private String _MonthCN the DateTime = .Now.ToString ( "MMMM", System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN"));
public ClassMonth()
DateTime.Now.ToString MonthCN = {( "MMMM", System.Globalization.CultureInfo.CreateSpecificCulture ( "the CN-ZH"));
MonthEN = DateTime.Now.ToString ( "MMMM", System.Globalization.CultureInfo.CreateSpecificCulture ( " EN "));
}
public ClassMonth (CN String, String EN)
{
MonthCN = CN; // import variable property assignment
MonthEN = en; // import variable property assignment
}
public String attribute configured MonthEN //
{
GET {
return _MonthEN;
}
SET
{
_MonthEN = value;
}
}
public String attribute configured MonthCN //
{
GET {
return _MonthCN;
}
set
{ _MonthCN = value;
}
}
}
绑定方法
程序代码
ArrayList arlist=new ArrayList();
arlist.Add(new ClassMonth("1月", "January"));
arlist.Add(new ClassMonth("2月", "February"));
arlist.Add(new ClassMonth("3月", "March"));
arlist.Add(new ClassMonth("4月", "April"));
arlist.Add(new ClassMonth("5月", "May"));
this.DropDownList4.DataSource = arlist;
this.DropDownList4.DataValueField = "MonthEN";
this.DropDownList4.DataTextField = "MonthCN";
this.DropDownList4.DataBind();
Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2011/07/28/2119219.html