Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

User Controls, postbacks and ViewState

Posted on 2010-08-20
7
Medium Priority
?
339 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 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
Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

 
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

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 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