ASP .NET: when UserControls loads?

2020-07-22 17:02发布

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.

标签: asp.net-2.0
3条回答
做个烂人
2楼-- · 2020-07-22 17:32

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.

查看更多
仙女界的扛把子
3楼-- · 2020-07-22 17:35

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.)

查看更多
够拽才男人
4楼-- · 2020-07-22 17:44

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.

查看更多
登录 后发表回答