2014-01-24 21 views
1

我想使用網格視圖的鏈接按鈕下載文件。但行命令不會觸發。網格視圖位於Ajax選項卡容器---更新面板中。 它在Normal窗體中工作正常,沒有ajax選項卡容器控件。GridView的行命令事件在ajax選項卡容器中不起作用

設計頁:

<asp:TabPanel ID="TabPanel2" runat="server" HeaderText="Download" Height="150px" Width="500px"> 
      <HeaderTemplate> 
       &nbsp; 

                                                   下載                                                                         

  <asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand" AutoGenerateColumns="False" 
       Height="80%" Width="70%"> 
      <Columns> 
      <asp:BoundField DataField="id" HeaderText="id" SortExpression="id"/> 
      <asp:BoundField DataField="filename" HeaderText="filename" SortExpression="filename"/> 

      <asp:TemplateField> 
      <ItemTemplate> 
       <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" CommandArgument='<%#Eval("id")%>' ForeColor="White" CausesValidation="true" >DownLoad</asp:LinkButton> 
      </ItemTemplate> 
      </asp:TemplateField> 

      </Columns> 
       <HeaderStyle BackColor="#FF6699" /> 
      </asp:GridView> 
      </ContentTemplate> 
      </asp:UpdatePanel> 
    </form> 


      &nbsp;<br /> 

      <br /> 
      <br /> 
      &nbsp; 
     </ContentTemplate> 
    </asp:TabPanel> 

代碼背後:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    int index = ((GridViewRow)((LinkButton)e.CommandArgument).Parent.Parent).RowIndex; 
    if (e.CommandName=="Download") 
     { 
      Tabs.ActiveTabIndex = 1; 
     int Id = Convert.ToInt32((e.CommandArgument).ToString()); 
     GridViewRow row = GridView1.Rows[Id]; 
     string filename = row.Cells[1].Text; 
     Response.ContentType = "application/octet-stream"; 
     Response.AddHeader("Content-Disposition", "attachment;filename=" + filename); 
     Response.TransmitFile(Server.MapPath("~/Docs/" +filename)); 
     Response.End(); 
} 

回答

0

需要設置下載鏈接爲PostbackControl,試試下面的代碼RowDataBound

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    LinkButton lb = e.Row.FindControl("LinkButton1") as LinkButton; 
    if(lb != null) 
    ScriptManager.RegisterPostbackControl(lb); 
} 
相關問題