ListBox,CheckBoxList,DropDownList,RadioButtonList的常见用法
四个都是选择控件,用法大同小异,基本都是指定键值对:
直接加选择项:
void way1() { DropDownList1.Items.Add("c#"); DropDownList1.Items.Add("vb"); DropDownList1.Items.Add("c"); DropDownList1.Items.Add("java"); ListBox1.Items.Add("c#"); ListBox1.Items.Add("VB"); ListBox1.Items.Add("C++"); ListBox1.Items.Add("JAVA"); CheckBoxList1.Items.Add("c#"); CheckBoxList1.Items.Add("VB"); CheckBoxList1.Items.Add("javascript"); CheckBoxList1.Items.Add("php"); RadioButtonList1.Items.Add("asp"); RadioButtonList1.Items.Add("php"); RadioButtonList1.Items.Add("jsp"); RadioButtonList1.Items.Add("ajax"); } 哈斯表法: void way2() { Hashtable ht=new Hashtable(); ht.Add("1","c#"); ht.Add("2","C++"); ht.Add("3","php"); ht.Add("4","CGI"); this.DropDownList2.DataSource=ht; DropDownList2.DataTextField="Value"; DropDownList2.DataValueField="Key"; DropDownList2.DataBind(); this.ListBox2.DataSource=ht; ListBox2.DataTextField="value"; ListBox2.DataValueField="key"; ListBox2.DataBind(); this.CheckBoxList2.DataSource=ht; CheckBoxList2.DataTextField="value"; CheckBoxList2.DataValueField="key"; CheckBoxList2.DataBind(); this.RadioButtonList2.DataSource=ht; RadioButtonList2.DataTextField="value"; RadioButtonList2.DataValueField="key"; RadioButtonList2.DataBind(); } SortedList方法: void way3() { SortedList s1=new SortedList(); s1.Add("1","haha"); s1.Add("2","hehe"); s1.Add("3","xixi"); s1.Add("4","heihei"); this.DropDownList3.DataSource=s1; DropDownList3.DataTextField="value"; DropDownList3.DataValueField="key"; DropDownList3.DataBind(); } 用数据对象dataread方法: void way4() { OleDbConnection cn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("sailing.mdb")+";Persist Security Info=False"); cn.Open(); OleDbCommand cmd=new OleDbCommand(); cmd.Connection=cn; cmd.CommandText="select RoleName,RoleID from Role"; OleDbDataReader dr=cmd.ExecuteReader(); while(dr.Read()) { ListItem li=new ListItem(dr[0].ToString(),dr[1].ToString()); this.ListBox4.Items.Add(li); } dr.Close(); cn.Close(); } 用数据集法: OleDbConnection cn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("sailing.mdb")+";Persist Security Info=False"); cn.Open(); OleDbCommand cmd=new OleDbCommand(); cmd.Connection=cn; cmd.CommandText="select RoleName,RoleID from Role"; OleDbDataAdapter adp=new OleDbDataAdapter(cmd); DataSet ds=new DataSet(); adp.Fill(ds); this.CheckBoxList4.DataSource=ds.Tables[0]; this.CheckBoxList4.DataTextField=ds.Tables[0].Columns[0].ToString(); this.CheckBoxList4.DataValueField=ds.Tables[0].Columns[1].ToString(); this.CheckBoxList4.DataBind(); 还有一种数组法: <%@ Page Language="C#" AutoEventWireup="True" %> <Script language="C#" runat="server"> void Page_Load(Object Sender,EventArgs e){ ArrayList array=new ArrayList(); array.Add("Windows"); array.Add("Linux"); array.Add("unix"); CheckBox_1.DataSource=array; CheckBox_1.DataBind(); RadioList_1.DataSource=array; RadioList_1.DataBind(); } </Script> <form id="Form_1" runat="server"> <ASP:checkboxList ID="CheckBox_1" runat="server"/> <input id="Button_1" type="Button" value="CheckButton" runat="server"/> <asp:radiobuttonlist ID="RadioList_1" runat="server"/> <input id="Button_2" type="Button" value="RadioButton" runat="server"/> </form>
时间: 2024-10-10 05:59:47