0

I am using the session object to store success/error messages based on user actions.

On each postback, the message is set on ItemCommend and retrieved on the Page_Load of the master page. Once retrieved, the message is deleted from the session.

The problem is that the master page's Page_Load gets called before the ItemCommand gets called so the message does not show up until the next refresh or postback.

How is this situation normally handled? Is there some other event we can code against?

Raheel Khan
  • 14,205
  • 13
  • 80
  • 168

3 Answers3

2

It is normal behavior of aspx and master pages. First of all content page's page load get fired after that Master Page's pageload get fired and then all other click etc.

You can use PageLoad Complete event to Solve your problem.

  • Thanks. Since I am using the MasterPage to display the message, it does not have that event. Only the Page class does. So I tried adding `this.Page.LoadComplete += new System.EventHandler(Page_LoadComplete);` where `this` is the MasterPage. The problem now is the event handler keeps on attaching additional copies of the Page_LoadComplete event on every `Page_Load`. – Raheel Khan Jun 17 '12 at 10:26
  • A quick workaround was to use `this.Page.LoadComplete -= new System.EventHandler(Page_LoadComplete);` followed by `this.Page.LoadComplete += new System.EventHandler(Page_LoadComplete);`. Is there a better way? Is there a way to declare the event in ASPX rather than code behind of the master page? – Raheel Khan Jun 17 '12 at 10:27
  • then try to use Master Page's PreRender Event. I think it should work for you. is a predefined event for master page. and it fires after Content Page load and Content Page PreRender. –  Jun 17 '12 at 11:02
  • One more alternate you can use that declare a Property on Master Page, and use that Property in Item Command of your control. Write your required code to update the session values in this property. –  Jun 17 '12 at 11:05
1

this is a normal behavior. show your message on itemcommand or Page_prerender

Raab
  • 34,778
  • 4
  • 50
  • 65
0

Create a Public Method in Code Behind of your master page like this:

 public void Set_Value(String SessionValue)
   {
      //your code here
   }

In aspx File of your content Page, Use following Line of code:

  <%@ MasterType VirtualPath="~/MasterPage.master" %>

Now in Code Behind of your Content Page, You can easily call Master Page's method in Item event of your any Control. In your Master Page's method you can write your required code to update and show values.

Call Master Page's Method on Content Page Like this:

  this.Master.Set_Value(Session["abc"].ToString());