When expanding asp.net web control, at what event should additional web controls be introduced?

I am trying to avoid a composite control or use and ASCX by extending an existing control. However, I am having problems adding controls to an inherited control and maintaining their presentation / state integrity after feedback. Whenever I add controls during pre-rendering, the controls are displayed, but post-back throws a viewstate exception. I tried to add them both there and during LoadViewState (which, of course, was stupid stupid). Init is not accessible from the control that I am expanding.

The exception is Sys.WebForms.PageRequestManagerServerErrorException: Failed to load viewstate. The control tree in which the loaded one should correspond to the control tree that was used to save the viewstate during the previous request. For example, when dynamically adding controls, controls added during feedback must match the type and position of controls added during Request

+3
source share
2 answers

In fact, microsoft says that you should override the CreateChildControls method .

You can call the base class method before or after adding controls, I'm not sure if there is an agreement there.

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

Hope this helps!

+4

OnInit CreateChildControls. , ViewState, . , "4. ".

+3

Source: https://habr.com/ru/post/1699678/


All Articles