简体   繁体   中英

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

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");
   }
}

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM