简体   繁体   中英

Row Command Event of GridView is not working in ajax tab container

I want to download a file with link button of grid view. but the row command does not firing. the grid view is in Ajax tab container---Update panel. Its working fine in Normal form without ajax tab container control.

Design Page:

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

Download

            <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>

Code Behind:

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

you need to set Download link as PostbackControl , try below code in RowDataBound

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM