如何在 ASP.NET 中打开从 Iframe 重定向的页面以在父窗口中打开?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果 iFrame 中的页面正在服务器端执行重定向,则 Response.Redirect 只会影响该页面。您希望在该 iFrame 中运行一些 javascript 来重定向父级,如第二个示例中所示。为了运行脚本,您不应该使用 Response.Redirect(),而应该注册客户端脚本。
有关如何在 ASP.Net 2.0 的代码中注册客户端脚本的信息,请参阅以下链接 -
在 ASP.Net 2.0 中使用 Javascript
例如,您可以在末尾添加与此类似的内容处理 ImageButton Click 的事件:
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:
我有一个 asp.net 页面,其中包含嵌入了一些数据和按钮的 Iframe。在按钮单击事件(服务器端)上,我有 Response.Redirct,但我需要关闭 Iframe 并加载父页面。添加下面提到的脚本解决了该问题。
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.
感谢 Rifk 提供的解决方案。对于那些有类似问题的人来说,这里是代码:
在aspx文件中,我定义了一个新的JS函数Redirection()。 ValidateFields() 函数将执行一些客户端验证。
在代码后面,我有非常简单的代码,在进行一些服务器端验证后注册 clientscriptblock。我要求仅当服务器端验证成功时才会发生重定向。
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.
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.
你可以试试这个:
问候。
You can try this:
Regards.
如果您只想使用 iframe(而不是新选项卡或窗口)直接在当前页面“上方”打开网站,那么您不需要隐藏代码。
即:
以及 ASPX 页面中的一行 Java 脚本代码...
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:
and a single line Java script bit of code in your ASPX page...