如何在 ASP.NET 中打开从 Iframe 重定向的页面以在父窗口中打开?

发布于 2024-10-18 02:57:53 字数 1223 浏览 1 评论 0原文

我有一个 asp.net 页面,其中包含嵌入了一些数据的 Iframe 和一个 ImageButton。在 ImageButton 单击事件(服务器端)上,我有 Response.Redirct:

Response.Redirect("results.aspx");

这始终在 iframe 中打开 results.aspx。我希望 results.aspx 应该始终在父窗口中打开。到目前为止,我尝试了以下方法,但没有成功:

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

正如 Rifk 所回复的,我添加了 ClientScriptManager。 .aspx 有此条目:

<asp:ImageButton ID="ImageButton_ok" ImageUrl="~/images/ok.gif"
        OnClick="btnVerify_Click" OnClientClick="ValidateFields()" 
        runat="server"  />

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 具有主要业务逻辑。如果我的业务逻辑失败,我将如何停止 OnClientClick() 触发?或者,当服务器端代码成功执行时如何触发?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

川水往事 2024-10-25 02:57:53

如果 iFrame 中的页面正在服务器端执行重定向,则 Response.Redirect 只会影响该页面。您希望在该 iFrame 中运行一些 javascript 来重定向父级,如第二个示例中所示。为了运行脚本,您不应该使用 Response.Redirect(),而应该注册客户端脚本。

有关如何在 ASP.Net 2.0 的代码中注册客户端脚本的信息,请参阅以下链接 -
在 ASP.Net 2.0 中使用 Javascript

例如,您可以在末尾添加与此类似的内容处理 ImageButton Click 的事件:

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

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);
倒带 2024-10-25 02:57:53

我有一个 asp.net 页面,其中包含嵌入了一些数据和按钮的 Iframe。在按钮单击事件(服务器端)上,我有 Response.Redirct,但我需要关闭 Iframe 并加载父页面。添加下面提到的脚本解决了该问题。

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); 
你没皮卡萌 2024-10-25 02:57:53

感谢 Rifk 提供的解决方案。对于那些有类似问题的人来说,这里是代码:

在aspx文件中,我定义了一个新的JS函数Redirection()。 ValidateFields() 函数将执行一些客户端验证。

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

在代码后面,我有非常简单的代码,在进行一些服务器端验证后注册 clientscriptblock。我要求仅当服务器端验证成功时才会发生重定向。

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

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);
}
抱着落日 2024-10-25 02:57:53

你可以试试这个:

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

问候。

You can try this:

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

Regards.

树深时见影 2024-10-25 02:57:53
Response.Clear();
Header.Controls.Add(new LiteralControl(@"
<script type=""text/javascript"">
top.location = ""/Logout.aspx"";
parent.location = ""/Logout.aspx"";
</script>
"));
Response.Clear();
Header.Controls.Add(new LiteralControl(@"
<script type=""text/javascript"">
top.location = ""/Logout.aspx"";
parent.location = ""/Logout.aspx"";
</script>
"));
愚人国度 2024-10-25 02:57:53

如果您只想使用 iframe(而不是新选项卡或窗口)直接在当前页面“上方”打开网站,那么您不需要隐藏代码。

即:

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

以及 ASPX 页面中的一行 Java 脚本代码...

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

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');
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文