CheckBoxList 的数据绑定及数据获取
2008-04-01 11:45
435 查看
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; //自己导入命名空间.
public partial class like : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack) //这句很重要,如果不加,则每次加载时都要执行一次绑定,易丢失数据.
{
//创建链接;
SqlConnection con = DB.createConnection(); //不再用 new
con.Open();
SqlCommand cmd = new SqlCommand("select * from [personlike]",con);
SqlDataReader sdr = cmd.ExecuteReader();
this.CheckBoxList1.DataTextField = "likeContent";
this.CheckBoxList1.DataValueField = "ID";
this.CheckBoxList1.DataSource = sdr;
this.CheckBoxList1.DataBind();
sdr.Close(); //关闭记录集
con.Close(); //关闭链接
}
}
//单击按钮读取所做的操作(数据),用Response.Write()方法显示.
protected void Button1_Click(object sender, EventArgs e)
{
for(int i=0;i<=this.CheckBoxList1.Items.Count-1;i++)
{
if(this.CheckBoxList1.Items[i].Selected)
{
Response.Write(this.CheckBoxList1.Items[i].Value.ToString() +"-"+this.CheckBoxList1.Items[i].Text + "<br>");
}
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; //自己导入命名空间.
public partial class like : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack) //这句很重要,如果不加,则每次加载时都要执行一次绑定,易丢失数据.
{
//创建链接;
SqlConnection con = DB.createConnection(); //不再用 new
con.Open();
SqlCommand cmd = new SqlCommand("select * from [personlike]",con);
SqlDataReader sdr = cmd.ExecuteReader();
this.CheckBoxList1.DataTextField = "likeContent";
this.CheckBoxList1.DataValueField = "ID";
this.CheckBoxList1.DataSource = sdr;
this.CheckBoxList1.DataBind();
sdr.Close(); //关闭记录集
con.Close(); //关闭链接
}
}
//单击按钮读取所做的操作(数据),用Response.Write()方法显示.
protected void Button1_Click(object sender, EventArgs e)
{
for(int i=0;i<=this.CheckBoxList1.Items.Count-1;i++)
{
if(this.CheckBoxList1.Items[i].Selected)
{
Response.Write(this.CheckBoxList1.Items[i].Value.ToString() +"-"+this.CheckBoxList1.Items[i].Text + "<br>");
}
}
}
}
相关文章推荐
- PPP 和PPPOE协议介绍
- 生命的意義
- Win32平台结构化异常与Win32平台多线程产生之间的关系
- Visual Studio菜单下不见“导出模板”菜单项
- cmd.exe - Windows Command Line Reference
- 《你必须知道的.NET》图书专题上线
- 今天决定看书考网络工程师!!
- C#利用自身函数启动外部程序
- ASP.NET生成静态页面实现方法
- 《Web设计艺术精髓》
- (视频)玩转Membership视频之五:底层三剑客:Membership、MembershipUser、Roles
- 初学eclipse,记录一些有关eclipse IDE使用的东东
- Vista下安装SQL Server 2005,附加数据库报错 的解决办法
- 关于fool
- SQL Server 2005架构中的一些基本概念模型
- Asp.NET生成静态页面并分页
- 又开始上班了
- Bill Gates
- 加大eclipse运行内存
- Concurrency and Race Condition in Linux 2.6