How to open the redirected page from Iframe to open in the parent window in ASP.NET?

Sri Reddy picture Sri Reddy · Feb 16, 2011 · Viewed 22.5k times · Source

I have an asp.net page that contains an Iframe embedded with some data and a ImageButton. On ImageButton click event (server side) I have Response.Redirct:

Response.Redirect("results.aspx");

This always open the results.aspx in iframe. I want that results.aspx should always open in the parent window. I tried the following till now but none worked:

Response.Redirect("<script language='javascript'>self.parent.location='results.aspx';</script>");
Response.Redirect("javascript:parent.change_parent_url('results.aspx');");

As responded by Rifk, I add the ClientScriptManager. .aspx has this entry:

<asp:ImageButton ID="ImageButton_ok" ImageUrl="~/images/ok.gif"
        OnClick="btnVerify_Click" OnClientClick="ValidateFields()" 
        runat="server"  />

Code behind in Page_Load():

ClientScriptManager cs = Page.ClientScript;
StringBuilder myscript = new StringBuilder();
myscript.Append("<script type=\"text/javascript\"> function ValidateFields() {");
myscript.Append("self.parent.location='default.aspx';} </");
myscript.Append("script>");
cs.RegisterClientScriptBlock(this.GetType(), "ButtonClickScript", myscript.ToString());

btnVerify_Click has the main business logic. How will I stop OnClientClick() to fire if there my business logic fails? or, how can I fire when server side code is successfully executed?

Answer

Dave Brace picture Dave Brace · Feb 16, 2011

Response.Redirect will only effect the page in the iFrame if that is the page that is doing the redirect on the server side. You want to run some javascript within that iFrame that will redirect the parent, as you have in your second example. In order to run the script, you shouldn't be using Response.Redirect(), but rather you should be registering client script.

See the following link as to how to register client script in your code in ASP.Net 2.0 - Using Javascript with ASP.Net 2.0

For example, you would add something similar to this at the end of your event that handles the ImageButton Click:

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myUniqueKey",
                    "self.parent.location='results.aspx';", true);