Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 738
Sri Reddy Avatar asked Dec 17 '22 17:12

Sri Reddy


2 Answers

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);
like image 56
Dave Brace Avatar answered Apr 26 '23 19:04

Dave Brace


I have an asp.net page that contains an Iframe embedded with some data and a buttons. On button click event (server side) I have Response.Redirct, but i need to close the Iframe and load the parent page.adding the below mentioned script solved the issue.

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myUniqueKey",                     "self.parent.location='results.aspx';", true); 
like image 39
praveen Avatar answered Apr 26 '23 20:04

praveen