4

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Sri Reddy
  • 6,832
  • 20
  • 70
  • 112

6 Answers6

8

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);
Dave Brace
  • 1,809
  • 2
  • 16
  • 20
  • 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? – Sri Reddy Feb 17 '11 at 13:45
  • On your image button you can set the OnClientClick property to run a javascript method, as explained here: http://www.w3schools.com/aspnet/prop_webcontrol_imagebutton_onclientclick.asp. Your javascript method would then have the statement you provided above such as: "self.parent.location='results.aspx';". – Dave Brace Feb 17 '11 at 16:23
  • 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. – Sri Reddy Feb 17 '11 at 20:54
  • 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. – Dave Brace Feb 17 '11 at 21:04
  • 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? – Sri Reddy Feb 17 '11 at 21:49
  • 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. – Dave Brace Feb 17 '11 at 21:55
  • 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!! – Sri Reddy Feb 18 '11 at 15:22
  • @Dave Brace - Thanks Dave, you saved my day. :) – Bibhu Apr 25 '13 at 07:56
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); 
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
praveen
  • 21
  • 1
  • 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 – w1ck3d64 Mar 21 '13 at 17:45
1

You can try this:

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

Regards.

user3334463
  • 81
  • 1
  • 1
1

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);
}
Sri Reddy
  • 6,832
  • 20
  • 70
  • 112
0
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

If you just want to open a website directly "over" the current page with your iframe (not new tab or window), then you don't need code-behind.

ie:

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

and a single line Java script bit of code in your ASPX page...

function OpenOverFrame() {
            window.open('http://mywebsite.com','_parent');
        }
Fandango68
  • 4,461
  • 4
  • 39
  • 74