tags:

views:

582

answers:

3

Is MasterPage -> Page -> UserControl the loading order of an ASP .NET request?
Is there a situation where an UserControl loads before the Page loads ?

I have private messages for my users and in every page they will see a message like this "You have 3 unread messages."
When users view an unread message I want to change the message to this "You have 2 unread messages."

This can be easily done with a Request.Redirect, but I want to avoid this.
message 1 -> click on it will goes to MarkAsRead.aspx?id=x wich redirects me to ViewMessage.aspx?id=x

Instead of doing this I want to mark the message as read from ViewMessage.aspx?id=x then decrease my Session variable "unreadMessages", then let my UserControl display the new number.

But my concern is that UserControls may not always load last.

+2  A: 

The event progression in ASP.NET is as follows:

Page.OnPreInit()
UserControl.OnInit()
MasterPage.OnInit()
Page.OnInit()
Page.OnInitComplete()
Page.OnLoad()
MasterPage.OnLoad()
UserControl.OnLoad()
Page.OnLoadComplete()
(page event handlers fired here)
Page.OnPreRender()
MasterPage.OnPreRender()
UserControl.OnPreRender()
Page.OnPreRenderComplete()
Page.OnSaveStateComplete()
UserControl.OnUnload()
MasterPage.OnUnload()
Page.OnUnload()

To accomplish what your trying to do, handle your message count update in your Page.Init event, then display that count during the UserControl.Load event. I've provided the full progression of events above, which may provide you more options in case the above scenario isn't adequate. (For example, you could update the count in Page.Load, and render it in UserControl.PreRender, if for some reason Page.Init was too soon.)

jrista
A: 

check this diagram for asp.net event order.

my suggestion: implement a public void UpdateInfo() in your UserControl which is called from both the UserControl's OnLoad() event and from the OnLoad() event in ViewMessage.aspx after it adjusts the counter for unread messages.

devio
Your suggestion is correct but I don't have access to my UserControl, it is a separate control loaded somewhere on the page that take the message count from a Session variable
pixel3cs
A: 

I would agree with the above answer - it can become very tricky when you rely on child user control load events. Consider how all the MS databound controls work - you need to tell them to Bind before they will perform their function.

Paddy

related questions