2

我创造了

<input type="checkbox" id="test" > 

使用文字。现在我想得到这个控件,所以我可以检查它是否被选中。如何在 aspx.cs 页面中找到此控件?

4

4 回答 4

5

如果你想找到对文件后面代码的控制,那么你应该将它设置为 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);
}
于 2010-11-03T08:30:21.470 回答
2

如果您以编程方式将其创建为文字,则无法使用 FindControl 来查找它。当表单回传时,您可以使用表单集合查看值是否回传,如:

Request.Form["test"]

或者

Request["test"]

如果用户不选中复选框,则表单值将不存在,这是使用隐藏字段来解决的问题。

HTH。

于 2010-10-29T12:35:32.860 回答
1

使用FindControl搜索您已为其指定 id 参数的服务器控件。

Control ctrl = FindControl("TextBox1");
于 2010-10-29T12:03:38.563 回答
0

尝试

Page.Controls.FindControl() 

或者

Page.YourFormNameHEre.Controls.FindControl()
于 2010-10-29T12:03:30.780 回答