3
votes

I am dynamically loading user controls to switch between views in my project. I am aware that I need to reload my user control on every postback. The odd thing is my viewstate for the control is maintained even though the control is gone? I confirm that the panel I loaded it into is empty and then I check the view state and my value is there.

What's stranger is that if I load a different control, it can see the viewstate from the previous control? I checked and my page cannot see viewstate from my dynamically loaded control and visa versa. This makes me think the user control is treated as its own page. The confusing part is why the second view I load can see values from the first and why the values are there even though I the control has disappeared?

I also find this section of the code to be useless. Why is it removing the control? The panel is always empty (the code is from Telerik):

string controlId = LatestLoadedControlName.Split('.')[0];
Control previousControl = pnlControlPlaceholder.FindControl(controlId);
if (!Object.Equals(previousControl, null))
{
   this.pnlControlPlaceholder.Controls.Remove(previousControl);
}

I looked at several posts and most say that viewstate is lost on every postback, although this is not the case for me. Perhaps because I'm using update panels. Although if an intial request handles an event and then reloads the same control again, the viewstate is lost. It only seems to preserve the viewstate on the very next postback.

Can anyone explain this odd behavior of sharing viewstate between user controls or why it is there even though the control is lost?

1
"why it is there even though the control is lost?" Maybe because the other control uses the same ID than the previous control. - Tim Schmelter
Where is the code setting the ID? - Bryan Crosby
Before it adds the user control to the panel: UserControl userControl = (UserControl)this.LoadControl(controlName); userControl.ID = userControlID.Replace("/", "").Replace("~", ""); ((IView)userControl).IsFirstLoad = bFirstLoad; this.pnlControlPlaceholder.Controls.Add(userControl); - KingOfHypocrites
Here's a good article from a few years back where the author takes a deep dive into viewstate handling: ViewState and Dynamic Control. It's a bit long but IMO a good read. - user1429080
@KingOfHypocrites where is userControlID set/defined? For the code that removes to control, the logic is to verify that the control exists in the collection before attempting to remove it. Whether this is necessary or not depends on how the controls are added to the Panel though by default if you are dynamically creating the controls they will only exist after you have added them. For the questions about view state behavior it would help if you can expand your question with more detailed steps that you take and when you are looking at the values in view state and how you look at them. - alhalama

1 Answers

0
votes

Apparently you can read viewstate between pages in two scenarios... Cross page postback and when using Server.Transfer. I believe the cross page postback scenario would explain what I am seeing.