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("","c#");
ht.Add("","C++");
ht.Add("","php");
ht.Add("","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("","haha");
s1.Add("","hehe");
s1.Add("","xixi");
s1.Add("","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[].ToString(),dr[].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[];
this.CheckBoxList4.DataTextField=ds.Tables[].Columns[].ToString();
this.CheckBoxList4.DataValueField=ds.Tables[].Columns[].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>

ListBox,CheckBoxList,DropDownList,RadioButtonList的常见数据绑定的更多相关文章

  1. js如何获取asp.net服务器端控件的值(label,textbox,dropdownlist,radiobuttonlist等)

    js如何获取asp.net服务器端控件的值(label,textbox,dropdownlist,radiobuttonlist等) 欢迎访问原稿:http://hi.baidu.com/2wixia ...

  2. 《ASP.NET1200例》嵌套在DataLisT控件中的其他服务器控件---DropDownList控件的数据绑定

    aspx <script type="text/javascript"> function CheckAll(Obj) { var AllObj = document. ...

  3. c#listbox使用详解和常见问题解决

    关于ListBox ListBox是WinForm中的 列表 控件,它提供了一个项目列表(一组数据项),用户可以选择一个或者多个条目,当列表项目过多时,ListBox会自动添加滚动条,使用户可以滚动查 ...

  4. Web控件文本框Reset的功能

    在前一篇中<怎样实现Web控件文本框Reset的功能>http://www.cnblogs.com/insus/p/4120889.html Insus.NET只实现了文本框的功能.单个或 ...

  5. Farseer.net轻量级开源框架 中级篇:数据绑定

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: DbFactory数据工厂 下一篇:Farseer.net轻量级开源框架 中级篇: ...

  6. ASP.NET数据绑定

    数据绑定是ASP.NET提供的另一种访问数据库的方法.与ADO.NET数据库访问技术不同的是:数据绑定技术可以让程序员不关注数据库连接.数据库命令以及如何格式化这些数据以显示在页面上等环节,而是直接把 ...

  7. ASP.NET数据绑定控件

    数据绑定控件简介 数据绑定分为:数据源 和 数据绑定控件 两部分,数据绑定控件通过数据源来获得数据,通过数据源来隔离数据提供者和数据使用者,数据源有:SqlDataSource,AccessDataS ...

  8. ASP.NET数据绑定控件简介

    •数据绑定分为数据源和数据绑定控件两部分(①数据绑定控件通过数据源获取和修改数据②数据绑定控件通过数据源隔离数据提供者和数据使用者)数据绑定控件→数据源→数据库•数据源:SqlDataSource(连 ...

  9. ASP.NET 数据绑定到列表控件

    <div> <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox> ...

随机推荐

  1. 测试jdbc连接下,mysql和mycat的吞吐性能

    最近一个项目需要数据库有较大的吞吐量,因为项目要求的访问量和数据量较大,决定采用一个数据库中间件来对数据库进行管理.经过一番查询,决定使用阿里的一个开源项目-mycat.因为mycat基于mysql, ...

  2. bzoj4247挂饰——DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4247 就是01背包: 把挂钩数限制在n以内,因为不需要更多,而这会带来一些问题,就是有很多挂 ...

  3. Git简单教程

    该笔记总结廖雪峰Git教程, 参考网站: https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017 ...

  4. js联动

    html: <!-- 省 --> <div class="col-sm-2"> <select name="p_id"> & ...

  5. awk里面执行shell命令

    先把文件列表存在filename文件中 先 awk '{system("rm $0")}' filename -------WRONG 因为对于 system来说 $0 不再是某行 ...

  6. vim opencv

    http://blog.csdn.net/fdl19881/article/details/7275203 ctags .vim: http://www.vim.org/scripts/script. ...

  7. POJ - 3414 Pots BFS(著名倒水问题升级版)

    Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...

  8. CodeForces - 357C Knight Tournament 伪并查集(区间合并)

    Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...

  9. 前端之CSS2

    CSS盒子模型 CSS盒子模型介绍 盒子模型解释 元素在页面中显示成一个方块,类似一个盒子,CSS盒子模型就是使用现实中盒子来做比喻,帮助我们设置元素对应的样式. 盒子模型示意图如下: 把元素叫做盒子 ...

  10. ugui学习资料

    官网文档 http://docs.unity3d.com/Manual/index.html 官网视频教程http://unity3d.com/learn/tutorials/modules/begi ...