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

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

Response.Redirect("results.aspx"); 

This always opens results.aspx in the iframe. I want result.aspx to always open in the parent window. I have tried the following so far, but no one has worked:

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

As Rifk answered, I am adding 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 core business logic. How can I stop OnClientClick () to start if my business logic fails? or, how can I run when the server code is successfully executed?

+4
source share
6 answers

Response.Redirect will only affect the page in the iFrame if it is a page that performs server-side redirection. You want to run some javascript inside this iFrame, which will redirect the parent, as in the second example. To run the script, you should not use Response.Redirect (), but you need to register the client script.

See the following link on how to register a 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, which ImageButton Click processes:

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

I have an asp.net page that contains an Iframe embedded with some data and buttons. On a button click event (server side) I have Response.Redirct, but I need to close the iframe and load the parent page. In the script below, the problem has been resolved.

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

Thanks to Rifk for the solution. Here is the code for those who have a similar problem:

In the aspx file, I defined a new JS () redirect function. The ValidateFields () function performs some client-side checks.

 <head runat="server"> <title>Untitled Page</title> <script language="javascript" type="text/javascript"> function ValidateFields() { alert ("Some client side validations!!"); } function Redirection() { self.parent.location="http://www.google.com"; } </script> </head> <body> <form id="form1" runat="server"> <div> <h2>Content - In IFrame</h2> <asp:CheckBox ID="chkValid" runat="server" /> <asp:ImageButton ID="ImageButton_FillW8Online" ImageUrl="~/images/expand.gif" OnClick="btnVerify_Click" OnClientClick="return ValidateFields()" runat="server" style="height: 11px" /> </div> </form> </body> 

in the code behind, I have very simple code that registers a clientcriptblock after doing some server side checks. I need the redirection to be performed only if the server-side check was successful.

 bool isValid = false; protected void Page_Load(object sender, EventArgs e) { } protected void btnVerify_Click(object sender, EventArgs e) { //do some validations isValid = chkValid.Checked; if (isValid) this.Page.ClientScript.RegisterClientScriptBlock(typeof(string), "", "Redirection()", true); } 
+1
source

You can try the following:

 Response.Write("<script>window.open('page.aspx','_parent');</script>"); 

Sincerely.

+1
source
 Response.Clear(); Header.Controls.Add(new LiteralControl(@" <script type=""text/javascript""> top.location = ""/Logout.aspx""; parent.location = ""/Logout.aspx""; </script> ")); 
0
source

If you just want to open the website directly β€œon top” of the current page using an iframe (not a new tab or window), then you don't need the code.

t

 <asp:LinkButton ID="lnkGeneralEnq" runat="server" OnClientClick="OpenOverFrame();"><strong>click this link</strong></asp:LinkButton> 

and one line of Java script bit of code on your aspx page ...

 function OpenOverFrame() { window.open('http://mywebsite.com','_parent'); } 
0
source

Source: https://habr.com/ru/post/1340031/


All Articles