当 RenderControl ascx 在 RenderBeginTag 中覆盖面板时无法 FindControl
我正在使用 Panel
控件创建一个容器,如下所示:
public class CustomContainer : Panel
{
public override void RenderBeginTag(HtmlTextWriter writer)
{
var control this.Page.LoadControl("web user control path.ascx");
control.ID = "userControlId";
control.RenderControl(writer);
base.RenderBeginTag(writer);
}
public void ShowMessage(string message)
{
var control = this.FindControl("userControlId"); // control here is null!!
var custom = control as CustomControl;
custom.Message = message;
}
}
当我尝试查找我呈现的 id userControlId
的控件时,它总是返回 null! 有谁知道发生了什么事?我该如何解决这个问题?
顺便说一句:我无法在 CreateChildControls
方法中添加 CustomControl
,因为如果 CustomContainer
有代码块,我就会遇到异常!
无法修改 Controls 集合,因为该控件 包含代码块(即 <% ... %>)。
I'm creating a container using Panel
control as follows:
public class CustomContainer : Panel
{
public override void RenderBeginTag(HtmlTextWriter writer)
{
var control this.Page.LoadControl("web user control path.ascx");
control.ID = "userControlId";
control.RenderControl(writer);
base.RenderBeginTag(writer);
}
public void ShowMessage(string message)
{
var control = this.FindControl("userControlId"); // control here is null!!
var custom = control as CustomControl;
custom.Message = message;
}
}
when I try to find the control with id userControlId
which I rendered, it always retuns null!
Does anyone know what's happening? How can I solve this issue?
BTW: I can't add the CustomControl
in CreateChildControls
method because if the CustomContainer
has code block I got an exception!
The Controls collection cannot be modified because the control
contains code blocks (i.e. <% ... %>).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能在呈现控件之前调用
ShowMessage
。尝试在OnPreLoad
或OnLoad
期间调用ShowMessage
。基本上,在渲染
之后的任何地方。You're probably calling
ShowMessage
before the control is rendered. Try callingShowMessage
duringOnPreLoad
orOnLoad
. Basically, anywhere after theRender
.