我创造了
<input type="checkbox" id="test" >
使用文字。现在我想得到这个控件,所以我可以检查它是否被选中。如何在 aspx.cs 页面中找到此控件?
我创造了
<input type="checkbox" id="test" >
使用文字。现在我想得到这个控件,所以我可以检查它是否被选中。如何在 aspx.cs 页面中找到此控件?
如果你想找到对文件后面代码的控制,那么你应该将它设置为 runat="server",
literal.text = "<input type=\"checkbox\" id=\"forum1\" runat=\"server\">";
HtmlInputCheckBox test = (HtmlInputCheckBox) Page.FindControl("test");
但是每当页面要回发时,您就会丢失此控件的状态。
愿这会给你正确的解决方案 http://www.codeasp.net/blogs/SumitArora/microsoft-net/841/value-of-dynamic-textbox-lost-on-postback
您可以使用页面初始化事件来生成控件
protected override void OnInit(EventArgs e)
{
HtmlInputCheckbox test = new HtmlInputCheckbox ();
test.id= "test";
pnlControl.Controls.Add(test);
base.OnInit(e);
}
如果您以编程方式将其创建为文字,则无法使用 FindControl 来查找它。当表单回传时,您可以使用表单集合查看值是否回传,如:
Request.Form["test"]
或者
Request["test"]
如果用户不选中复选框,则表单值将不存在,这是使用隐藏字段来解决的问题。
HTH。
使用FindControl搜索您已为其指定 id 参数的服务器控件。
Control ctrl = FindControl("TextBox1");
尝试
Page.Controls.FindControl()
或者
Page.YourFormNameHEre.Controls.FindControl()