【问题标题】:open new page in new window in gridview buttonfield在gridview按钮字段的新窗口中打开新页面
【发布时间】:2012-12-07 12:15:36
【问题描述】:

我的 asp.net 页面中有 gridview。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" CssClass="Gridview"
                    OnRowCommand="GridView1_RowCommand">
                    <Columns>
                        <asp:ButtonField Text="VIEW" ButtonType="link" CommandName="view" />
                    </Columns>
                </asp:GridView>

我想在新窗口中打开新页面。

因为我使用了下面的代码。(这个代码不起作用!-请检查是否有任何错误)

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("view"))
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow gvrow = GridView1.Rows[index];
        String id= gvrow.Cells[1].Text;
        string url = "~/Mypage.aspx?myid=" + id;
        Response.Write("<script>window.open( 'www.google.com' , '-blank' );</script>");
    }
}

我在运行时在 GRIDVIEW 中绑定数据 请记住这一点。

这样我就不能使用超链接字段了。

建议我使用 gridview 中的编码在新窗口中打开新页面的方法。

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    迟到总比从不好...我自己也遇到了同样的问题,所以我在这里发帖让其他编码人员可能会得到帮助。

    首先我使用的是链接按钮而不是按钮域...

    <ItemTemplate>
       <asp:LinkButton ID="viewLink" runat="server" CommandName="Select"  >
           <img class="pdficon" src="../Pictures/pdf_icon.png" />
       </asp:LinkButton>
    </ItemTemplate>
    

    (有一张图片可以点击在新窗口中打开文档,忽略它)

    我的 gridview 有以下代码...(gridview documentGridView 上的名称)

    OnSelectedIndexChanged="openLinkClick"
    DataKeyNames="docfolder"
    

    在我的代码隐藏中,当我请求新的“默认”页面时,我将指向新页面的链接作为参数传递

    protected void openLinkClick(object sender, EventArgs e)
    {
        //lots of code for constructing link, the important thing is SelectedDataKey
        string docId = documentsGridView.SelectedDataKey.Value.ToString();    
        //passing link as parameter when opening a new default page with given
        //dimensions for new window
        ClientScript.RegisterStartupScript(this.Page.GetType(), "", "window.open('Document.aspx?param1=" + link + "','Graph','height=900,width=1100');", true);
        //refresh page
        Page_Load(this, null);
    }
    

    最后在 Document.aspx 的 Page_Load 中,我有以下代码用于获取链接并打开我的文件,因为您应该可以获取链接并进行重定向。

    public partial class Document : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Open pdf in browser
            Response.ContentType = "Application/pdf";
            //get the parameter containing the adress for the file
            string link = Request.QueryString["param1"];
            //open the file
            Response.WriteFile(link);
            Response.End();
        }
    }
    

    也许不是最简单的解决方案,而是一种解决在客户端而不是服务器端打开的空白页面的方法。

    【讨论】:

      【解决方案2】:

      替换您的代码

      Response.Write("<script>window.open( 'www.google.com' , '-blank' );</script>");
      

      ClientScript.RegisterClientScriptBlock(this.GetType(), "Message", "window.open('www.google.com','_blank');", true);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-06
        • 2020-12-13
        • 2011-02-03
        • 1970-01-01
        • 2017-03-29
        • 1970-01-01
        • 1970-01-01
        • 2017-06-14
        相关资源
        最近更新 更多