如何遍历CheckBoxList,获得选中项的值?动态绑定CheckBoxList或给某些项赋值?2008-01-17 19:15CheckBoxList,相信大家应该并不陌生,特别是在投票里面,还有爱好啦什么的里面很有有用,下面我们就来探讨一下CheckBoxList的基本用法:
1.CheckBoxList的动态绑定
基本思路如下:
在Page_load里,加载页面的时候从数据库里读取Northwind数据库里表Employees表的值绑定到CheckBoxlist上.这样就把数据库的表的内容绑定好了.
if(!this.IsPostBack)
...{
SqlConnection con=new SqlConnection("server=.;database=Northwind;uid=sa;pwd=;");
con.Open();
SqlCommand cmd=new SqlCommand("select top 10 * from Employees",con);
SqlDataReader sdr=cmd.ExecuteReader();
try
...{
this.chklistA.DataSource=sdr;
this.chklistA.DataTextField="LastName";
this.chklistA.DataValueField="EmployeeID";
this.chklistA.DataBind();
}
catch(Exception Ex)
...{
throw Ex;
}
finally
...{
sdr.Close();
con.Close();
con.Dispose();
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余1页未读,立即下载
- 1
- 2
前往页