Solved

User Controls, postbacks and ViewState

Posted on 2010-08-20
7
324 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 9

Expert Comment

by:puru1981
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
It is not the answer I was looking for.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now