Problem with Master pages event order - asp.net

I have a simple setup with the master page housing some controls used by all child pages.
I found when moving to new pages the master page page loads event fires as a non post back and read the solution was to store it's current values somewhere for retrieval. Ok all done.
The child page uses these values to run a report. When I switch to a new report, all is well. If I change the values in the master page the master page and the sub page load events fire.
The load event for the sub page fires first, picks up the values from the master page which are still the old values and then finally the master page events fire and all the new values are stored. The report hasn't changed as it still ran from the old values.
I can't really see a way around this. All you ever hear is that master pages are a saving grace but I swear i've never jumped through so many hoops to get a page to load correctly.
And now this!
Anyone see a plan to resolve it?

Populating the controls during the Masterpage's Init will solve your issue from the sounds of it.
http://msdn.microsoft.com/en-us/library/dct97kc3.aspx
An alternate approach would be to have a public sub in the content page(s) that you can call from the masterpage during load which in effect acts as an alternate to the page load event.

A slightly more indepth look at the page lifecycle when using masterpages:
http://weblogs.asp.net/ricardoperes/archive/2009/03/08/asp-net-page-events-lifecycle.aspx

Related

asp.net master page and viewstate

I have some values that are set on a master page and that I want to save across a postback. I then want these variable to be available to pages using that master page during their load events.
Easy enough to create properties on the master page. So my first try to was to say that during the master page's load event, if not ispostback then generate the values and save them to the viewstate, else read them from the viewstate.
Except ... apparently the regular page load event happens BEFORE the master page load event, so the data wasn't there yet when I tried to read it.
Second try: have the master page set or retrieve these values during the Init event. No luck. Appears that the view state is not populated by Init time.
As far as I can tell, there's no event on a master page that happens after view state is populated but before the main page's Load event.
I suppose each page could have an InitComplete or PreLoad that calls a function to populate these fields, but that seems really clumsy. The call would have to be in every page. And it would have to be in every page even if that page never used this data, because the master page uses the data for its own purposes.
Is there a way to do this? Maybe view state is not the right place to save the data? I could store the data in Session variables, but then on not-postback the data in them would be left over from the last call. I guess I could make sure to clear the obsolete data, but that seems really clumsy.
I'm writing in VB but I wouldn't think that would make a difference here.
You could override LoadViewState method of the master page in the following manner:
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
// use the loaded values, if any, here
}
LoadViewState is called before Load and in fact even before PreLoad of the page, so that seems to fit what you are looking for.

Asp.net output cache substitution with postback in user control

We have a site where all pages are output cached, i.e. the caching is on aspx-level with VaryByParam="*". Now there is a requirement to make a gallup control, i.e. a small "How is this site working for you?" and then when the user clicks an answer the results are shown.
The gallup is implemented as a usercontrol that is added to the master page so gallups can be added to any page to which a gallup is created in the cms. The problem is that output cache naturally caches all clicks so when user no 2 votes he sees the results that where calculated after the first vote on that alternative.
Now I'm trying to use cache substition. I added an asp:Substition tag where the user control used to be, load the control dynamically and render it (using this approach http://coderwall.com/p/4ajzqq). The problem is that the postbacks that voting triggers is never fired. Apparently the method that the substition control executes is loaded outside the page life cycle or too late.
Any ideas?
Regards,
Mathias
I solved this by skipping the substitution control and just used the user control as normal, so the gallup/question view was output cached. Then in the click handler for the answer alternatives I added
Response.Cache.SetNoServerCaching();
which exempted the results view from output cache and hence it was updated as it should.
It is also possible to use Response.Cache.SetNoServerCaching() in Global.asax, using VaryByCustom. It merely requires sensing in Global.asax whether the page is a post-back or not. Here is a code example.

In what page life phase should I add external javascript?

If I want to add external javascript file into an aspx page in back end code page, in what page life should I add it? Page_Load? or Page_PreInit?
As far as I know it doesn't mind where you put it. I usually put them in the the Page_PreRender event.
There are a couple of reason why I do this.
you want to conditionally add Javascript resources based on some condition in your page, so your need your whole page to be loaded. The PreRender event is the perfect time to check some conditions because everything should be loaded and ready to be renderd.
If you for example add them in the PreInit and in your PageLoad you decide to redirect to a different page you executed unnecessary code.
So even if both points do not apply to the situation I still put them in the PageRender as it's the guideline for the whole project.

dynamically load user controls using jquery ajax

I've read several articles on this issue, and technically speaking, all they end up doing is taking html code from a user control and injecting it into the containing page. I want a true user control to be loaded on to the page, so that on postback, I still have access to that loaded user control and I can validate fields on it on server side, etc. etc. What I mean to say is that once the user control has been loaded onto the page using ajax, it should there after act like it was originally created as part of page life cycle when the page was first loaded. Does that make sense? Any ideas on how to do this? UpdatePanel may be a solution, but I'd rather not use that.
Update panel is the solution since you need it to update the page's ViewState so that the newly added usercontrol needs to be part of the page lifecycle. Also, when the page does post back, you need to remember that it was added and re-add it so that the control structure gets re-created properly.
When you dynamically create user controls, you have to make sure to bind to the elements. Look into http://docs.jquery.com/Events/live

How to translate content page in a master page after language change (ASP.NET)

I have a content page in a master page in ASP.NET.
What is going on is that the user click on a button in the right corner of the master page and that change the current language. What does the button is changing the Session for the new selected language.
What is happening is that when you click on a button, the code is executed AFTER the Page_Load of both the master page and the content page is called. But in my button, I call a function "Translate" that do the translation of the master page.
Unfortunately, I don't know what to do for the content page. I tried to do a manual reload of it but I don't know how.
Is there anyone here that can help me? I don't think that code is needed here but if it can help you, just ask me what you need and I will update.
Thanks
Jaff
After changing the users language settings (both on the thread and where ever you persist that information too) do a Response.Redirect to the page they are already on. This allows for a clean page load and reduces the chance that other culture specific settings will cause a problem (such as date and time selections.)

Resources