2017-02-13 74 views
0

我正在創建一個web應用程序。在我的應用程序中,我的GridView中有一個gridview和一個鏈接按鈕。我的LinkBut​​ton看起來是這樣的:在gridview c中使用鏈接按鈕#

<asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%# Eval("FileData") %>' runat="server" OnClick="lnkDownload_Click"></asp:LinkButton> 

在我的桌子上有一個鏈接,每一個文件,就像(~\userpic\chart.png

當鏈接按鈕用戶點擊下面的代碼應該運行

protected void lnkDownload_Click(object sender, EventArgs e) 
{ 
    string filePath = (sender as LinkButton).CommandArgument; 

    if(string.IsNullOrEmpty(filePath)) 
    { 
     ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "", "alert('No File to download.');", true); 
     return; 
    } 
    Response.ContentType = ContentType; 
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath)); 
    Response.WriteFile(filePath); 
    Response.End(); 
} 

但是當我運行代碼時,我無法下載該文件。當我調試這個方法時,調試斷點沒有被擊中。我的代碼有什麼問題?

+0

勾選此鏈接的'處理鏈接按鈕點擊事件直接點擊事件代碼'部分:[鏈接](http://www.dotnetbull.com/2013/05/how-to-handle-click-event-of -linkbutton.html) – RRM

+0

也發佈'GridView'代碼。 –

回答

1

上你的鏈接按鈕添加一個CommandName的屬性

<asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%# Eval("FileData") %>' runat="server" OnClick="lnkDownload_Click" **CommandName="Download"**></asp:LinkButton> 

,並在你的行命令事件

protected void YourGridview_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     if (e.CommandName == "Download") 
     { 
      /*your code to download here */ 
     } 
    } 
0

當你打算顯示此asp:LinkButton gridview的行裏面,這OnClick事件將不開火。您必須爲gridview提供OnRowCommand="GridView_RowCommand" 屬性,並在GridView_RowCommand()方法內寫入代碼OnClick而不是lnkDownload_Click()。我希望這會起作用。試試看。

相關問題