Solved

User Controls, postbacks and ViewState

Posted on 2010-08-20
7
333 Views
Last Modified: 2013-11-08
Hi,

I am dynamically loading User Controls onto a page based on a specific URL parameter (foobar.aspx?pageid=3 for example) and adding the User Controls to a PlaceHolder control on the page. In the PageLoad event in if not page.ispostback then I add the URL parameter to the ViewState i.e. ViewState("id")=3.

I understand that in order for the ViewState to get loaded back into the dynamically added User Controls, the User Controls need to be dynamically added again.

If my understanding is correct, this re-adding of User Controls has to happen before the Page Load event. If that is the case, how do I get the ViewState("id") value if the re-adding happens before the PageLoad event, for instance in the Page_Init. I don't think the ViewState is available from this event.

My workaround is to just request the URL parameter in the Page_Init again. i.e. I do GetUserControls(Request.Querystring("id")) as opposed to GetUserControls(ViewState("id")).

Is there a better way? Is this a valid way of doing this? Any help is greatly appreciated.
0
Comment
Question by:MonCapitan
  • 3
  • 3
7 Comments
 
LVL 19

Expert Comment

by:daveamour
ID: 33483804
ViewState is loaded before Page_Load I think:
http://www.15seconds.com/issue/020102.htm
 
0
 
LVL 9

Accepted Solution

by:
puru1981 earned 500 total points
ID: 33483889
if it is not a postback then viewstate is not there but if it is a postback the viewstate exist.

if you are adding a control in page_init you can provide the id for the usercontrol which you can use in without viewstate. you can use page.findcontrol to get the usercontrol and its properties anywhere in the application.
0
 

Author Comment

by:MonCapitan
ID: 33484618
Thanks for your replies.

I only know which User Control (UC) to load in by getting that information from ViewState when there is a postback.

So, I can't load in the ViewState into the UC until I have loaded in the UC which relies on being able to access the ViewState to know which UC to load. It's a mind bender.

Have you any other ideas?
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 9

Expert Comment

by:puru1981
ID: 33484829
may i know what you are trying to do?

if you are opening a page on click of button then you can send the information through the query string or other means.

if you are on the same page then you will be doing the same on the some event bubbled up by some control's change or click event. in that case you have the viewstate and you can add the values to viewstate after loading the usercontrol.

safer option will be to use session to store the data and clear the session after closing the page.
0
 

Author Comment

by:MonCapitan
ID: 33484968
Regarding..."you can add the values to viewstate after loading the usercontrol. "...is there a built in function that I can call that the UC has. For example,

dim myUC as UserControl = LoadControl("bla.ascx")
MyUC.LoadViewStateIntoChildControls()

I hope that makes sense?
0
 
LVL 9

Expert Comment

by:puru1981
ID: 33485458
in this case i think you need to load the whole page using response.redirect and add the control in page init but do use query string or session to pass the values between calls.

secondly create a div with all the user controls with visible property false. and set the user control's visibility true when required.

0
 

Author Closing Comment

by:MonCapitan
ID: 33510660
It is not the answer I was looking for.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question