开发者

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

开发者 https://www.devze.com 2023-02-11 02:02 出处:网络
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:

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 al开发者_运维技巧ways 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?


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


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


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


You can try this:

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

Regards.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消