GridView的行命令事件在ajax选项卡容器中不起作用

时间:2014-01-24 06:26:38

标签: c# asp.net ajax gridview ajaxcontroltoolkit

我想下载一个带有网格视图链接按钮的文件。但是行命令没有触发。网格视图位于Ajax选项卡容器---更新面板中。 它在Normal模式下工作正常,没有ajax tab容器控件。

设计页面:

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

1 个答案:

答案 0 :(得分: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);
}