View and user controls in asp.net

I have a question about viewstate and user control in asp.net.

Say I have a page and a simple composite control. I know that in a composite control, I have to load all of my child controls on the .OnInit page so that their viewstate can be applied and be ready for OnLoad.

Now let's say that I have a tree on the page and based on the selected node I would like to load a specific user control. TreeView selectedNode is not available during OnInit, but is available in OnLoad and after. I also know that if I add a user control on the OnLoad page of the page, in which the customs control cycle will still start with OnInit, and then on OnLoad, etc. Etc.

So my question is, if the OnInit user control is still being called, even if I load this control into the OnLoad parent method, why is it not populated for the user control?

Is it because the parent contains a view for the children, and if I load the children from the parent OnLoad, then the viewport is not available?

How do you usually load user controls if you have the situation described above (with a tree)?

+4
source share
1 answer

ASP.NET loads the viewstate for the child controls, even if they are added to the page in OnLoad , as shown in this example:

 protected override void OnLoad(EventArgs e) { Literal literal = new Literal(); this.placeHolder.Controls.Add(literal); if (!this.IsPostBack) literal.Text = "I'm still here after a postback."; } 

Note that by default, ASP.NET loads the viewstate based on the indexes of the child controls, so make sure the order of your controls is consistent with one writeback to the next.

+1
source

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


All Articles