ListBox,CheckBoxList,DropDownList,RadioButtonList的常见用法

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>

转载于:https://www.cnblogs.com/youchun/archive/2009/12/27/1633655.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值