Loadcontrol and viewstate question

J

John

Hi all,

I am using dynamic user controls within my web app and these controls are
loaded into placeholders via the LoadControl method.

My problem is this:
I have usercontrolA loaded into a placholder and the user clicks on a
specific control outside of the usercontrol and some Javascript is run where
a hidden variable is changed to reflect another usercontrol (usercontrolB).

My main page fires upon postback and then checks the request variable (i.e.
hidden control) and then uses that variable to reload usercontrolB into the
placeholder upon which the app raises an exception :

Failed to load viewstate. 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.
At this point, I know I don't want to use any of the viewstate that has been
stored.

My question is this:
What do I have to do to prevent this problem from occurring?

Can't I somehow clear the viewstate prior to loading usercontrolB?

Regards
John.
 
K

Karl Seguin

No, you can't. What you might be able to do is disable the viewstate for
the usercontrolA in the first place:

usercontrolA.EnableViewState=False
myPlaceHolder.Controls.Add(usercontrolA);

If that doesn't work, you might be able to make it work by disabling the
viewstate for the entire page (if you can)

If you can't do either of those (or they don't work, cuz I'm not 100% sure),
you need to reload userControlA on postback, set it to invisible (or try
removing it) and then load usercontrolb

Karl
 
J

John

Hi Karl

The problem is that I need the viewstate within my usercontrols.

I've found a rather convoluted workaround involving using request and
session variables in conjunction with redirecting the page and it is
working.

The main problem I have is that no-one, but on-one seems to really
understand how loading dynamic user controls actual should work. The people
at the company I went on a .NET course with don't even know. I'm going to
attempt (in the coming weeks) to contact someone at Microsoft SA and see if
it's possible to arrange a meeting with them to bring some sort of closure
to my long-standing dilemma.

<Watch this space>

Regards
John.
 
W

William F. Robertson, Jr.

Why don't you still load UserControlA into the placeholder, but set the
Visible property to false, when you don't need to see it?

bill
 
J

jhewitt

It seems the problem is this:

When you load a user control onto the page the control 'values' are
stored in viewstate. When the page reloads, the user control no longer
exists (just its viewstate). When a new control is loaded in the old
controls place, it can't negotiate the old controls viewstate so the
app clocks.

I Had the same problem
I would try to use a 'main page' and have it load different user
controls into its 'content' section. I'll quickly describe what I'm
talking about so you will get a better idea of what I did.

My main page has a 'loadContent' procedure. This procedure gets a
control name from session and loads that control into a panel. When a
user clicks a button on the user control, the code behind the user
control sets the session variable to a new control name and everything
posts back. On the post back, the main page runs 'loadContent' and
gets the new control name from session. Loading this new control
causes the app to crash.

My Solution
I added another session variable called 'loadedControl'. Now when the
main page's 'loadControl' method is called, if the control name sent to
it differes from the value in 'loadedControl', the previously loaded
user control is reloaded (assuming it's viewstate) and removed
(removing its viewstate with it). Then the new user control is loaded
into the panel on the main page. See my 'loadedControl' procedure in
VB below:

Public Sub loadContent(ByVal controlName As
UserControls.enumContent)

Me.pnlContent.Controls.Clear() ' just in case

' adds the previously loaded control, if different from the one
sent, and removes it to clean up viewstate
If controlName <> GlobalVariables.loadedContent Then

Me.pnlContent.Controls.Add(UserControls.content(GlobalVariables.loadedContent))
Me.pnlContent.Controls.Remove(Me.pnlContent.Controls(0))
End If

' loads sent control
Me.pnlContent.Controls.Add(UserControls.content(controlName))

' sets the global variable name for the finally loaded control
GlobalVariables.loadedContent = controlName

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top