Welcome to weblogs.com.pk Sign in | Join | Help

Nested User Controls

Today while winding up my current project; I wanted to export everything as a single user control that can be used in any ASP.NET v2 web application; by just dropping the control on the page. For this I needed to implemented nested user controls; that is my exposed user control loads other helper user controls as the visitor flows across the interface. Here are few notes

  • Always use PlaceHolder to load the children controls; don’t use any other controls.
  • Always set the ID of the control before adding it into PlaceHolder’s control collection.

Control c = this.Page.LoadControl(controlFile);
if (null != c)
  c.ID = control +
this.selectedControl = controlFile; //this is view state powered private propery to track which control is loaded

  • You have to load all the controls on each postback; you may use viewstate to track which control needs to be loaded
  • IsPostBack property in the children control would not work for tracking when first time your control is loaded, here is the work-around I used

bool isLoaded
    bool r = false;
    object o = this.ViewState["_isLoaded"];
    if (null != o)
      r =
    if (!r)
      this.isLoad = true;
    return r;
    this.ViewState["_isLoaded"] = value;


IsPostBack thing is logical; it should be true on each successive postbacks. My point was that if you are using it for tracking the first time your user control is loading; it will not work; you have to invent your own mechanism; something similar to what I already have mentioned.

Published Tuesday, January 24, 2006 6:44 PM by khurram
Filed under:


No Comments

New Comments to this post are disabled