如何在gridview的rowcommand事件中的新选项卡中打开页面? [英] How to open a page in a new tab in the rowcommand event of gridview?

查看:23
本文介绍了如何在gridview的rowcommand事件中的新选项卡中打开页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

protected void gv_inbox_RowCommand(object sender, GridViewCommandEventArgs e)
{
    int index = Convert.ToInt32(e.CommandArgument);

    if (e.CommandName == "sign")
    {
        Session["TransYear"] = int.Parse(((HiddenField)gv_inbox.Rows[index].Cells[1].FindControl("HDN_TransYear")).Value);
        Session["MailNumber"] = int.Parse(((HiddenField)gv_inbox.Rows[index].Cells[1].FindControl("HDN_MailNumber")).Value);
        Response.Redirect("Signature.aspx", false);
        //Response.Write("<script>");
        //Response.Write("window.open('Signature.aspx','_blank')");
        //Response.Write("</script>");
    }
}

我想在新标签页或窗口中打开页面.注释代码会这样做,但是当 refresh 原始页面导致错误时.how to open Signature.aspx in a new window or tab in the row command event in a new window or tab in the row command event我的网格视图.

I want to open the page in a new tab or window . The commented code do that but when refresh the original page cause errors .how to open Signature.aspx in a new window or tab in the correct way in the row command event of my gridview .

推荐答案

您要做的是使用 ScriptManager 进行 JavaScript 调用.

What you want to do is use the ScriptManager to make the JavaScript call.

您定义的 JavaScript 代码段有效,但是,您需要 ScriptManager 来为您执行它.

The JavaScript snippet you had defined works, however, you need the ScriptManager to take care of executing it for you.

String js = "window.open('Signature.aspx', '_blank');";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Open Signature.aspx", js, true);

使用Response.Write 的问题在于,在PostBack 期间,浏览器将执行脚本标签.另一方面,当使用 ScriptManager.RegisterClientScriptBlock 时,ASP.NET ScriptManager 会自动执行代码.

The issue with using Response.Write is that during PostBack, the browser will not execute script tags. On the other hand, when using ScriptManager.RegisterClientScriptBlock, the ASP.NET ScriptManager will execute the code automatically.

更新 - 2013/02/14

正如 Vladislav 的回答中所建议的,使用 ClientScriptScriptManager;区别在于 ClientScript 仅用于 同步 回发(没有 asp:UpdatePanel),而 ScriptManager 用于异步回发(使用 asp:UpdatePanel).

As suggested in Vladislav's answer, use ClientScript vs. ScriptManager; the difference is ClientScript is for synchronous postbacks only (no asp:UpdatePanel), and ScriptManager is for asynchronous postbacks (using asp:UpdatePanel).

此外,根据作者下面的评论 - 将 asp:GridView 包含在 asp:UpdatePanel 中将消除确认刷新的浏览器消息.同步回发(无asp:UpdatePanel)后刷新页面时,浏览器会重新发送上一条命令,导致服务器再次执行相同的过程.

Additionally, per the author's comment below - enclosing the asp:GridView in an asp:UpdatePanel will eliminate the browser message confirming a refresh. When refreshing a page after a synchronous postback (no asp:UpdatePanel), the browser will resend the last command, causing the server to execute the same process once more.

最后,当将 asp:UpdatePanelScriptManager.RegisterClientScriptBlock 结合使用时,请确保将第一个和第二个参数更新为 asp:UpdatePanel 对象.

Finally, when using an asp:UpdatePanel in conjunction with ScriptManager.RegisterClientScriptBlock, make sure you update the first and second parameters to be the asp:UpdatePanel object.

ScriptManager.RegisterClientScriptBlock(updatePanel1, updatePanel1.GetType(), "Open Signature.aspx", js, true);

这篇关于如何在gridview的rowcommand事件中的新选项卡中打开页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆