如何在GridView中使用ButtonField打开新的选项卡/页面

本文关键字:选项 页面 ButtonField GridView | 更新日期: 2023-09-27 17:59:07

<asp:GridView ID ="FileGrid" runat="server" AutoGenerateColumns="False" OnRowCommand="FileGrid_RowCommand">
    <Columns>
        <asp:BoundField DataField="OriginalFileName" HeaderText="OriginalFileName" SortExpression="OriginalFileName" /> 
        <asp:BoundField DataField="AttachmentGUID" HeaderText="AttachmentGUID" SortExpression="AttachmentGUID" />             
        <asp:ButtonField Text="Generate PDF" runat="server" HeaderText="Convert To PDF" CommandName="GeneratePDF_Click" />
    </Columns>
</asp:GridView>

代码背后:

protected void FileGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "GeneratePDF_Click")
    {
        int num = Convert.ToInt16(e.CommandArgument);
        string attachmentGuid = FileGrid.Rows[num].Cells[1].Text;
        Response.Redirect("DisplayImage.aspx?AttachmentGUID=" + attachmentGuid);
    }
}

我看到很多使用OnClientClickasp:Button的解决方案,但我不能在列中使用asp:Button

如何在GridView中使用ButtonField打开新的选项卡/页面

您可以使用ScriptManager.RegisterStartupScript打开一个新窗口:

var url = "DisplayImage.aspx?AttachmentGUID=" + attachmentGuid;
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "popup", "window.open('" + url + "','_blank')", true);

或者你也可以试试:

var url = "DisplayImage.aspx?AttachmentGUID=" + attachmentGuid;
string redirect = "<script>window.open('" + url + "');</script>"; 
Response.Write(redirect);