CyberK
CyberK

Reputation: 1578

ASP.Net masterpage Response.Redirect before child call

I have a masterpage and inside that masterpage I have to check if a session is null or not. If the session is null, then there has to be a redirect to a login page.

That is no problem, but the problem is that the masterpage also have controls using the session and the child also uses the session so I get a nullreferenceexception.

I now have this:

protected void Page_init(object sender, EventArgs e)
{
   var session = (ServiceSession)Session["serviceSession"];

   if (session == null)
   {
         Response.Redirect("login.aspx", false);
   }
}

But the problem is that the controls on the masterpage are also called instead of redirecting immediatly.

So the main question:

How can I redirect immediatly, without loading further things. Because the page_init gets called as first method the redirect should solve my nullreferenceexception, but than I don't need to load all components etc.

Thanks in advance!

Upvotes: 1

Views: 4642

Answers (2)

Ivo
Ivo

Reputation: 3436

You can use the Page_PreInit for this

private void Page_PreInit(object sender, EventArgs e) {

Response.Redirect("login.aspx", false);

}

Upvotes: -1

DavidGouge
DavidGouge

Reputation: 4623

Have you tried setting the second param of your Response.Redirect to true to halt the execution of the page?

Upvotes: 3

Related Questions