【发布时间】:2011-12-14 05:03:35
【问题描述】:
在dayrender事件中添加控件后,以后有没有办法找到该控件?我试过了
calendar.FindControl("lblSample")
但没有成功。
这里是我的一些代码更清楚:
protected void calSample_DayRender(object sender, DayRenderEventArgs e)
{
Label lblSample = new Label();
lblSample.ID = "lblSample";
lblSample.Text = "Sample";
e.Cell.Controls.Add(lblSample);
}
在一天的渲染事件和页面完全加载之后,我有一个链接按钮事件,我尝试在其中取回控件
protected void lbtnSave_Click(object sender, EventArgs e)
{
//Not working
Label lblSample = calSample.FindControl(lblSample);
//Also can't get to work, this was using Ross' suggestion and the recursive find function he wrote about. I'm probably just not using it correctly.
Label lblSample = ControlFinder.FindControl<Label>(calSample, "lblSample");
}
【问题讨论】:
-
FindControl不会递归搜索,因此您需要自己创建不仅搜索当前子节点,还搜索其中任何容器的子节点。 -
您可能需要通过多个层进行递归,例如如果你的表单有一个 asp:Panel 有你的控制,你需要导航 Form => Panel => Control.
标签: c# asp.net .net findcontrol