2012-12-07 221 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(); 
    } 
} 

也許不是最好的解決方案,而是解決空白頁打開客戶端和服務器端的一種方法。

相關問題