7

I know this isn't possible, but what is the best alternative for wanting to do Response.Redirect from an iFrame, to redirect the parent page?

Curtis
  • 101,612
  • 66
  • 270
  • 352

4 Answers4

11

You can not do this using ASP.NET. ASP.NET on server side can redirect incoming request and can't know about a parent frame.

But if you want to redirect parent frame on some server side condition you can call JavaScript from server like this:

protected void Page_Load(object sender, EventArgs e) {
   ClientScriptManager.RegisterClientScriptBlock(this.GetType(), 
       "RedirectScript", "window.parent.location = 'http://yoursite.com'", true);
}

And of course you can use simple JavaScript window.parent.location = 'http://yoursite.com' on client side.

Baidaly
  • 1,829
  • 1
  • 15
  • 16
  • 1
    Note that the domains of the frame and parent frame documents will need to match to avoid an access denied error being thrown by most browsers. – Chris Feb 21 '13 at 11:02
  • If my parent has another domain in the frame? – Kiquenet Sep 13 '17 at 06:51
8

I just used the following code with success. It even bypassed the X-Frame-Options SAMEORIGIN and allows redirection from one domain to another one in an iframe:

string url = "https://siteurl.com";
Response.Write("<script>top.location='"+url+"';parent.location='"+url+"';</script>");

With string interpolation (since C# 6):

string url = "https://siteurl.com";
Response.Write($"<script>top.location='{url}';parent.location='{url}';</script>");
krlzlx
  • 5,752
  • 14
  • 47
  • 55
2
Response.Clear();
Header.Controls.Add(new LiteralControl(@"
<script type=""text/javascript"">
top.location = ""/Logout.aspx"";
parent.location = ""/Logout.aspx"";
</script>
"));
mjb
  • 7,649
  • 8
  • 44
  • 60
0
    url = "Your.asp?YourVar="&YourVar
    %> \end your classic asp code and pass to the script below
    <script type='text/javascript'>window.top.location.href = '<%= url %>'</scrip>
James Nelson
  • 61
  • 13