R – When extending a asp.net web control, at which event should additional web controls be injected

asp.netvb.netweb-controls

I'm trying to avoid a composite control or using and ASCX by extending an existing control. However, I'm having trouble with getting the controls added to the inherited control and keep their view-state/post-back integrity. Whenever I add the controls during pre-render, the controls show up, but the post-back throws a viewstate exception. I tried adding them both there and during LoadViewState (which of course was a long-shot silly). Init is not available from the control which I'm extending.

The exception is
Sys.WebForms.PageRequestManagerServerErrorException:
Failed to load viewsstate. The control
tree into which viewstate is being
loaded must match the control tree
that was used to save viewstate during
the previous request. For example,
when adding controls dynamically, the
controls added during a post-back must
match the type and position of the
controls added during the initial
request

Best Answer

Actually, microsoft says you should override the CreateChildControls method.

You can call the base class method before or after you add the controls, I'm not sure if there is a convention there.

protected override void CreateChildControls(){
  Controls.Add(someControl);
  base.CreateChildControls();
}

Hope that helps!

Related Topic