By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,677 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,677 IT Pros & Developers. It's quick & easy.

Loadcontrol and viewstate question

P: n/a
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.
Nov 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"John" <a@b.c> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
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.

Nov 19 '05 #2

P: n/a
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

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"John" <a@b.c> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
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.

Nov 19 '05 #3

P: n/a
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.
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
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

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"John" <a@b.c> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
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.


Nov 19 '05 #4

P: n/a
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
"John" <a@b.c> wrote in message
news:e%****************@TK2MSFTNGP09.phx.gbl...
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.
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
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

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying) http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"John" <a@b.c> wrote in message
news:eh**************@TK2MSFTNGP09.phx.gbl...
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.



Nov 19 '05 #5

P: n/a

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(Gl obalVariables.loadedContent))
Me.pnlContent.Controls.Remove(Me.pnlContent.Contro ls(0))
End If

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

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

End Sub

--
jhewitt
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 19 '05 #6

P: n/a
I recently posted a sample on how to do this. I don't use User Controls,
but they should work just fine:

http://groups-beta.google.com/group/...717a97bd32c450

-Brock
DevelopMentor
http://staff.develop.com/ballen
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.


Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.