我的网页正在加载时,我正在创建5个单选按钮:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
for (int i = 0; i < 5; i++)
{
RadioButton r = new RadioButton();
r.Text = i.ToString();
r.ID = i.ToString(); ;
Panel1.Controls.Add(r);
}
}
}
我想用与单击按钮相对应的另一种方法来访问它们,但我不能。:
protected void Button1_Click(object sender, EventArgs e)
{
RadioButton r = (RadioButton)FindControl("2");
r.Checked = true;
}
当我执行findcontrol方法时,出现以下异常:用户代码未处理nullreferenceexception
FindControl
不进行深度搜索。您添加单选按钮Panel1
,但调用FindControl
的Page
。
RadioButton r = (RadioButton)Panel1.FindControl("2");
另一件事。删除if (!Page.IsPostBack)
条件。当Button1_Click
火灾,页面在回发和动态控制必须建立,如果你希望找到他们。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句