C# 单击按钮时获取动态复选框值
我有一个页面,其中动态创建了两个复选框C# 单击按钮时获取动态复选框值,c#,asp.net,dynamic,checkbox,C#,Asp.net,Dynamic,Checkbox,我有一个页面,其中动态创建了两个复选框 TableRow tr = new TableRow(); for (int i = 0; i < 2; i++) { TableCell Tc = new TableCell(); Tc.Attributes["style"] = "line-height: 3
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);
}
但是在这种情况下,我该如何检查呢
谢谢
答复:
这是获取复选框值所需的操作
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);
}
}
}
}
protectedvoid Update1\u单击(对象发送方,事件参数e)
{
for(int i=0;i
只要在page\u PreInit方法中将复选框添加到页面中,这应该可以正常工作。如果在此之后添加它们(例如页面加载),则不会保留它们的值
在此处阅读有关asp.net页面生命周期的信息:
考虑将动态复选框存储在本地成员中:
private CheckBox _myCustomCheckbox = new CheckBox();
protected override void OnInit(EventArgs e)
{
TableRow tr = new TableRow();
for (int i = 0; i < 2; i++)
{
TableCell Tc = new TableCell();
if (i == 0)
{
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
_myCustomCheckbox.ID = "checkBoxCtrl" + j;
Tc.Controls.Add(_myCustomCheckbox);
tr.Cells.Add(Tc);
}
}
// the row needs added to a page control so that the child control states can be loaded
SomeTableOnThePage.Controls.Add(tr);
base.OnInit(e);
}
protected void Update2_Click(object sender, EventArgs e)
{
if(_myCustomCheckbox.Checked)
{
response.write("true");
}
}
private CheckBox\u myCustomCheckbox=new CheckBox();
受保护的覆盖无效OnInit(事件参数e)
{
TableRow tr=新的TableRow();
对于(int i=0;i<2;i++)
{
TableCell Tc=新的TableCell();
如果(i==0)
{
Tc.Attributes[“style”]=“行高:30px;文本对齐:左”;
Tc.属性[“宽度”]=“50%”;
Tc.Style.Add(“左填充”、“5px”);
//左侧带有标签的复选框
_myCustomCheckbox.ID=“checkBoxCtrl”+j;
Tc.Controls.Add(_myCustomCheckbox);
tr.Cells.Add(Tc);
}
}
//该行需要添加到页面控件,以便可以加载子控件状态
SomeTableOnThePage.Controls.Add(tr);
碱基.奥尼特(e);
}
受保护的无效更新2\u单击(对象发送方,事件参数e)
{
如果(_myCustomCheckbox.Checked)
{
回答:填写(“真实”);
}
}
可能不是您想要的,但我有一个类似的问题,我在ASP.NET页面中有一个动态生成的表,在一列中有一个动态生成的复选框。我已经从一个集合中为表创建了数据,然后在创建动态CB时,我给它们一个ID,并将它们存储在第二个集合中,例如CB的数组
因此,当我需要查找选中的值时,我只需遍历集合,就可以找到选中的值
另外,由于它们是与动态表中的数据同时创建的,所以我能够轻松地将表数据行绑定到复选框值
这显然假设动态表和CB是使用某种循环创建的
这可能不是最好的解决方案,但适合我当前的需要。您需要在会话中为该用户保存复选框,并在每次页面加载时添加复选框,并确保保留对它们的代码隐藏引用。它也可以用javascript来完成,所以如果你想要的话,它会在客户端执行。。但是,在创建页面之后,假设我选中复选框并单击确定按钮,我如何检查是否找到具有动态复选框id的复选框并从中获取值?您现在正是这样做的。我想说的是,尽管您仍然会看到复选框,并且如果在Page_Load上创建它,您可以单击它,但是除非您将其添加到Page_PreInit中的页面,否则您将无法正确检查它的值。我需要在Page_init和Page_Load中创建,还是只使用Page_init?如果页面加载发生了怎么办。。谢谢谢谢,这很好。如果有多个复选框,我该如何选择呢?请您查看我的编辑,因为我将有两个id为checkBoxCtrl0和checkBoxCtrl1的复选框,我需要同时选中这两个复选框。。。如果我解决了这个问题,我就可以走了
private CheckBox _myCustomCheckbox = new CheckBox();
protected override void OnInit(EventArgs e)
{
TableRow tr = new TableRow();
for (int i = 0; i < 2; i++)
{
TableCell Tc = new TableCell();
if (i == 0)
{
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
_myCustomCheckbox.ID = "checkBoxCtrl" + j;
Tc.Controls.Add(_myCustomCheckbox);
tr.Cells.Add(Tc);
}
}
// the row needs added to a page control so that the child control states can be loaded
SomeTableOnThePage.Controls.Add(tr);
base.OnInit(e);
}
protected void Update2_Click(object sender, EventArgs e)
{
if(_myCustomCheckbox.Checked)
{
response.write("true");
}
}