473,854 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2750
No, you can't. What you might be able to do is disable the viewstate for
the usercontrolA in the first place:

usercontrolA.En ableViewState=F alse
myPlaceHolder.C ontrols.Add(use rcontrolA);

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******** ******@TK2MSFTN GP09.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.En ableViewState=F alse
myPlaceHolder.C ontrols.Add(use rcontrolA);

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******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP09.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.En ableViewState=F alse
myPlaceHolder.C ontrols.Add(use rcontrolA);

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******** ******@TK2MSFTN GP09.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%******** ********@TK2MSF TNGP09.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******** ********@TK2MSF TNGP09.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.En ableViewState=F alse
myPlaceHolder.C ontrols.Add(use rcontrolA);

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******** ******@TK2MSFTN GP09.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(ByV al controlName As
UserControls.en umContent)

Me.pnlContent.C ontrols.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.C ontrols.Add(Use rControls.conte nt(GlobalVariab les.loadedConte nt))
Me.pnlContent.C ontrols.Remove( Me.pnlContent.C ontrols(0))
End If

' loads sent control
Me.pnlContent.C ontrols.Add(Use rControls.conte nt(controlName) )

' 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
1847
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 BasePage. In my BasePage class, I have an OnInit method defined as shown: protected override void OnInit(System.EventArgs e) { this.Controls.AddAt(0, LoadControl("_header.ascx")); base.OnInit(e);
2
1484
by: sunil | last post by:
Will the state of a dynamically loaded control preserved in transitions between aspx pages hosting the control.? thanks
1
2124
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. unfortunately, none of the controls, except a label, have any values. what am i not doing? all the controls on the WUC have enableviewstate set to true. thanks,
0
483
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 not work the first time they are attempted. Every subsequent attemp works great. Here is what is happening private void Page_Load(object sender, System.EventArgs e) { LoadContentControl();
1
1777
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 properties declared on it over and above that provided by the textbox. As I understand it, part of the process to make this work is, in the property accessor's for the properties that you wish to persist you add
1
1552
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 the main grid contains a button which expands the nested grid for that row on the server- side. I am loading the usercontrol when the user has pressed that button. Now the problem is when the user has one grid already expanded and wants to expand...
8
5252
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 the constructor for example MyContorl oMyControl = new MyContorl(employeeid). However I need to load the control at runtime so the have to call it this way LoadControl("MyControl.ascx") and I get an error that I have not supplied any parameter...
0
352
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 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).
1
1803
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 bestimmtes UserControl (Kunden.ascx) dynamisch per LoadControl mehrmals einbinden. Dabei soll dem UserControl jeweils eine andere Variable (Anfangsbuchstabe des Kunden) übergeben werden (z.B. per Session-Variable) übergeben werden.
0
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9751
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10679
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10371
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5741
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5941
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3186
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.