asp.net 获取checkboxlist值

2024-12-21 15:08:03
推荐回答(2个)
回答1:

//循环新建CheckBox控件
for (int i = 0; i < 3; i++)
{
CheckBox cb = new CheckBox();
cb.Text = "第" + i + "个";
cb.Checked = true;
Label1.Controls.Add(cb);
}
//循环访问Label里面的控件
foreach (var item in Label1.Controls)
{
//如果控件为CheckBox就执行
if (item is CheckBox)
{
//先将控件转换为CheckBox以调用它的Checked属性
CheckBox cb = item as CheckBox;
//输出勾选的CheckBox
if (cb.Checked == true)
Response.Write(cb.Text);
}
}

回答2:

public void bindTP()
{
DataSet ds = ps.GetAllTP();
GridView2.DataSource = ds;
GridView2.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Cookies["aa"] != null)
{
foreach (GridViewRow myrow in GridView2.Rows)
{
RadioButton cc = (RadioButton)myrow.Cells[0].FindControl("RadioButton1");
if (cc.Checked)
{
Response.Write("");
cc.Checked = false;
return;
} }
}
else
{
foreach (GridViewRow myrow in GridView2.Rows)
{
RadioButton cb = (RadioButton)myrow.Cells[0].FindControl("RadioButton1");
if (cb.Checked)
{
int TP_ID = Convert.ToInt32(myrow.Cells[1].Text);
int i = ps.Updata_TP_Sum(TP_ID);
if (i > 0)
{
Response.Cookies["aa"].Value = Request.UserHostAddress.ToString();
Response.Cookies["aa"].Expires = DateTime.Now.AddMinutes(1);
Response.Write("");
cb.Checked = false; } } 这是我做投票时的源码,希望能对你有帮助