2012-12-07 223 views
0

我在我的asp.net页面中有gridview。在gridview buttonfield的新窗口中打开一个新页面

<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中的编码在新窗口中打开新页面。

回答

1

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

替换你的代码

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

它不在GRIDVIEW中工作。 ! :( – Patel

+0

你得到了哪个错误? –

+0

没有错误,但是没有任何操作在按钮上点击。 – Patel

2

犹未晚之后再也没有......我自己,所以我在这里发布的其他编码者或许得到帮助了同样的问题。

首先,我用的LinkBut​​ton而不是ButtonField字段...

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

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

相关问题