Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

User Controls, postbacks and ViewState

Posted on 2010-08-20
7
Medium Priority
?
345 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 1500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

886 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