0

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

Ahmed Magdy
  • 5,956
  • 8
  • 43
  • 75

1 Answers1

0

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

James Johnson
  • 45,496
  • 8
  • 73
  • 110