Ruby
Ruby

Reputation: 969

How to Redirect a page, outside of an Iframe using c#

These are the Pages in the application

  1. Login.aspx ( doesnt use any master )
  2. Home.aspx (Uses the master).

There is an IFrame in the master which loads all the webpages like page1.aspx, page2... When a user is browsing say page1.aspx and session expires, the login page loads inside the IFrame, whereas, it should load externally, I mean outside of the iFrame.

I hope my question made some sense

Page_Init()
{
  if(Session["user"]==null)
   {
      Response.Redirect("~/Forms/Login.aspx");
   }
}

There are solutions here for JS as window.top.location.href="..", but dont know how to implement. how to fix this using C#.

Upvotes: 0

Views: 3610

Answers (3)

Bhupendra Shukla
Bhupendra Shukla

Reputation: 3914

There are many ways if we uses client side scripts but by using Non Client Side Scripts:

<a href="location" target="_top">Click here to continue</a> 

<a href="location" target="_parent">Click here to continue</a>

Upvotes: 2

Abhinav
Abhinav

Reputation: 2085

This should work

Page_Init()
{
  if(Session["user"]==null)
   {
      string jScript = "window.top.location.href = '/Forms/Login.aspx';";
      ScriptManager.RegisterStartupScript(this, this.GetType(), "forceParentLoad", jScript, true);
      //Response.Redirect("~/Forms/Login.aspx");
   }
}

Upvotes: 2

Kinexus
Kinexus

Reputation: 12904

An aspx page within an Iframe has no reference to it's parent without using javascript. Therefore, you will need to implement it as javascript.

If the session expires, you could simply write out the JS at the same point you were attempting to Response.Redirect and it should work fine.

Upvotes: 1

Related Questions