【发布时间】:2011-10-24 03:11:03
【问题描述】:
我有一个页面,我在其中动态创建 2 个复选框。
TableRow tr = new TableRow();
for (int i = 0; i < 2; i++)
{
TableCell Tc = new TableCell();
Tc.Attributes["style"] = "line-height: 30px; text-align: left";
Tc.Attributes["width"] = "50%";
Tc.Style.Add("padding-left", "5px");
//Checkboxes on left along with labels
CheckBox checkBoxCtrl = new CheckBox();
checkBoxCtrl.ID = "checkBoxCtrl" + i;
Tc.Controls.Add(checkBoxCtrl);
tr.Cells.Add(Tc);
}
一旦它们在页面加载事件中创建,我就会有一个 Ok_button 点击事件,它需要检查复选框是否被选中。
protected void Update2_Click(object sender, EventArgs e)
{
if(checkBoxCtrl.checked)
//here i wont be able to get the value
// i get the error the name checkBoxCtrl does not exist..
{
response.write("true");
}
}
但是在这种情况下我该如何进行检查。
谢谢
答案:
这是获取复选框值需要做的事情
protected void Update1_Click(object sender, EventArgs e)
{
for(int i = 0; i < ControlPropList.Count; i++)
{
CheckBox chkTest = (CheckBox)xxx.FindControl("checkBoxCtrl" + i);
{
if (chkTest.Checked)
{
Global.logger.Info("Checkbox True = " + chkTest.ID);
}
else
{
Global.logger.Info("Checkbox False = " + chkTest.ID);
}
}
}
}
【问题讨论】:
-
您需要在 Session 中为该用户保存复选框,并在每次页面加载时添加复选框,并确保保留对它们的代码隐藏引用。它也可以用 javascript 来完成,所以它在客户端上执行,如果你想要的话。
标签: c# asp.net dynamic checkbox