views:

26

answers:

1

I have a application where I am trying to redirect on session time out so therefore in my master page I am checking if session variable is null for the redirection but the problem is that I have other pages(derived) from masterpage and on Page_Load of derived pages i am referencing some session variables there also and I've observed that PAGE_LOAD event of derived pages(content) fires first and master page PAGE_LOAD fires later so the error is popping "Object reference not set".

By the way I am writing following code on LOGIN_BUTTON_PRESSED EVENT.

FormsAuthenticationTicket ticket - new FormsAuthenticationTicket(1, userName, DateTime.Now, DataTime.Now.AddMinutes(20), true, myRoles, FormsAuthentication.FormsCookiePath);

Session["uid"] = userName.Text;  
Session["ufullname"] = ufname;  

string hashCookies = FormsAuthentication.Encrypt(ticket);  

HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);    

Response.Cookies.Add(cookie);  

Response.Redirect("~/Main.aspx");

Please suggest a solution to overcome this problem with example.

A: 

One option is to move the code in your master page Page_Load event, to the Page_Init event:

protected void Page_Init( object sender, EventArgs e )
{
    //Your code goes here
}

See ASP.NET Page Life Cycle Overview

Tchami
Tchami you got my question wrong. The above code I've pasted to tell that i am using FormsAuthentication. I am checking Session variables on MasterPage (Page_Load) event so it can inherit to content page also but contents (Page_Load) is triggering first so therefore Object Reference error is popping. Now what do you guys suggest.
I don't think I did. The Page_Init event will fire before the Page_Load event, so if you move your code in the master page Page_Load event, to the Page_Init event in your master page, you should not be getting any object refererence errors on your content pages.
Tchami
Well let me explain..in my MasterPage.master.cs file and Page_Load event I am writing if (Session["uid"] == null || Session["ufullname"] == null) { Response.Redirect("~/Default.aspx"); }And in the content page "MyProfile.aspx.cs" Page_Load event i am writing. ((Label)Master.FindControl("Label1")).Text = "My Profile";((Label)Master.FindControl("ufname")).Text = "Welcome, " + Session["ufullname"];lbl_usertext.Text = Session["ufullname"].ToString();As per your suggestions Ive moved the master page code into Page_Init event but still getting object ref error.
Which line is throwing the error?
Tchami
this line Session["ufullname"].ToString();
Actually when i leave the application idle for 20 mins and click on any of my content page link the page_loads fires which has the same line.
1. Make sure you're actually writing something to the Session variable.2. Please update your original post with the source code of the Page_Load and Page_Init events.
Tchami
Ok I have solved the problem by implementing the BASEPAGE class and Pre-Init event. thanks all of your for the valuable inputs.