Solved

User Controls, postbacks and ViewState

Posted on 2010-08-20
7
336 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

615 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