473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Loadcontrol and viewstate question

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
6 2704
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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: George Durzi | last post by:
I have a C# in my web application called BasePage. It inherits from System.Web.UI.Page. I'm using page inheritance in my web app so all my pages have an identical look and feel by inheriting from...
2
by: sunil | last post by:
Will the state of a dynamically loaded control preserved in transitions between aspx pages hosting the control.? thanks
1
by: Craig Buchanan | last post by:
i am building a webform that dynamically loads one of two web usercontrols during the webform's page_load event. during the postback i reload the appropriate web usercontrol. so far so good. ...
0
by: Mark | last post by:
I have a "Tabbed" ASP.NET page. When a user clicks on a tab, i want the corresponsing dynamic control to be loaded. This works great except that the postbacks on that dynamically loaded control do...
1
by: Simon | last post by:
Hi everyone, I have a quick question that I hope someone can help me with: I've made a user control that contains a text box and some validation functionality. This control has a few extra...
1
by: Anonymous | last post by:
Hello, I am loading a datagrig, which has a placeholder in it dynamically with a usercontrol. The usercontrol is actually another datagrid. Once loaded it looks like a nested grid. Each row in...
8
by: Sam Kuehn | last post by:
How do I accomplish the fallowing (is it even possible). Say I write a UserControl "MyControl.ascx". Now I use LoadControl("MyControl.ascx"). But I really want MyControl to require parameters in...
0
by: John | last post by:
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...
1
by: Andreas | last post by:
Hallo zusammen, ich kämpfe seit Tagen mit folgendem Problem und habe bisher leider keine Lösung gefunden. Vielleicht kann mir jemand helfen: Ich möchte auf einer Seite (test.aspx) ein...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.