简体   繁体   中英

Can't FindControl when RenderControl ascx in RenderBeginTag while overriding Panel

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 (ie <% ... %>).

You're probably calling ShowMessage before the control is rendered. Try calling ShowMessage during OnPreLoad or OnLoad . Basically, anywhere after the Render .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM