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

22,859

Solution 1

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

Solution 2

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

Solution 3

Thanks Rifk for the solution. Here is the code for those who have similar issue:

In aspx file, I have defined a new JS function Redirection(). ValidateFields() function will do some client side validations.

<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 code behind, I have very simple code that registers clientscriptblock after doing some server side validations. I required that the redirection to happen only if the server side validation is successfull.

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

Solution 4

You can try this:

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

Regards.

Share:
22,859
Sri Reddy
Author by

Sri Reddy

Updated on May 28, 2020

Comments

  • Sri Reddy
    Sri Reddy almost 4 years

    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?

  • Sri Reddy
    Sri Reddy about 13 years
    I am sorry, but somewhat lost. Not sure how to implement it. Where and what to write on my IFrame page? Do you want me to use Page.ClientScript.RegisterClientScriptBlock() Method? But how to wire it up with my ImageButton click event?
  • Dave Brace
    Dave Brace about 13 years
    On your image button you can set the OnClientClick property to run a javascript method, as explained here: w3schools.com/aspnet/…. Your javascript method would then have the statement you provided above such as: "self.parent.location='results.aspx';".
  • Sri Reddy
    Sri Reddy about 13 years
    Rifk, doing this will make the OnClientClick (client side code) run without waiting for my processing of OnClick (server side code). right? We have some business logic in OnClick that need to be executed before redirection. Thanks for time and the explanation.
  • Dave Brace
    Dave Brace about 13 years
    In that case, you'll want to just do Page.ClientScript.RegisterClientScriptBlock() at the end of the event that handles your ImageButton click event on the server.
  • Sri Reddy
    Sri Reddy about 13 years
    btnVerify_Click has the main business logic. How will I stop OnClientClick to fire if my business logic fails? or, how can I fire JS code only when server side code is successfully executed?
  • Dave Brace
    Dave Brace about 13 years
    If you only want the javascript to run after successful server side processing, remove the OnClientClick attribute and just add the code I included in my edit above at the end of successful processing of btnVerify_Click.
  • Sri Reddy
    Sri Reddy about 13 years
    thanks Rifk, I was able to resolve the issue. I have added the comple code below.. you can let me know if there is something which is wrong. :) have a nice day!!
  • Balaji Birajdar
    Balaji Birajdar over 12 years
    This is a good solution. It works in all browsers except Safari on Mac.
  • w1ck3d64
    w1ck3d64 about 11 years
    this worked great for me..was wondering if there was a way to have it open a new tab in the parent instead of just changing the page
  • Bibhu
    Bibhu about 11 years
    @Dave Brace - Thanks Dave, you saved my day. :)