遍历datalist中的checkbox,并获取此时的值
原文发布时间为:2009-04-29 —— 来源于本人的百度文章 [由搬家工具导入]
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=sa;database=Northwind");
SqlDataAdapter a = new SqlDataAdapter("select top 5 productname from products", conn);
DataSet ds = new DataSet();
a.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string num = "";
foreach (Control c in DataList1.Controls)
{
CheckBox chk = (CheckBox)c.FindControl("CheckBox1");
if (chk.Checked)
num += chk.Text+"<br>";
}
Response.Write(num);
}
两点注意:
1.要用服务器端控件checkbox
2.绑定的时候要写在if(!ispostback)条件下