Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

c# - How to open a page in a new tab in the rowcommand event of gridview?

I have the following code :

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

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 .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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.

UPDATE - 2013/02/14

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

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...