[Solved] Download file on ASP.Net LinkButton Click inside UpdatePanel not working

thereallover01
 
on May 26, 2022 01:59 AM
792 Views

I have a download button in GridView which is inside of update panel, on pressing the button there is no error but the file is not being downloaded, on research many told about post back trigger but since the button is inside GridView trigger is also not working.

Any help will be highly appreciated

Here the GridView

<asp:UpdatePanel ID="updtPnl" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ID="gvEducation" runat="server"
            Font-Size="Small"
            EmptyDataText="No records Found"
            AutoGenerateColumns="False"
            CssClass="table table-striped table-bordered table-hover table-responsive table-colored table-info"
            OnRowDataBound="gvEducation_RowDataBound">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="S.No" HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <%# Container.DataItemIndex + 1 %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="schoolCollege" HeaderText="Organization" />
                <asp:BoundField DataField="degree" HeaderText="Degree" />
                <asp:BoundField DataField="IMAGe_NAMe" HeaderText="Name" />
                <asp:BoundField DataField="EXT" HeaderText="EXT" />
                <asp:TemplateField HeaderText="Document">
                    <ItemTemplate>
                        <asp:Image ID="ImageActual" runat="server" Style="height: 50px; width: 50px; border-style: ridge; border-width: 5px;" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Document">
                    <ItemTemplate>
                        <asp:LinkButton ID="DocumentEducation" CssClass="btn btn-rounded btn-info" CommandArgument='<%# Eval("SNO") %>' Text="Download" runat="server" OnClick="DocumentEducation_Click"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />

            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="saveEducation" />
    </Triggers>
</asp:UpdatePanel>

Herez the code behind of download button

protected void DocumentEducation_Click(object sender, EventArgs e)
{
    try
    {
        LinkButton lnk = (LinkButton)sender;
        GridViewRow gr = lnk.NamingContainer as GridViewRow;

        int SNO = int.Parse((sender as LinkButton).CommandArgument);

        DataTable dt = blun.Education(SNO);
        string ext = dt.Rows[0]["ext"].ToString();
        string[] extsss = ext.Split('.');
        string last = extsss[extsss.Length - 1];
        Response.Clear();
        Response.Buffer = true;

        Response.ContentType = "application/" + last;
        Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["Image_Name"].ToString());
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.BinaryWrite((byte[])dt.Rows[0]["Image"]);
        Response.Flush();
        //Response.End();
    }
    catch (Exception ex)
    {
        string error = ex.ToString();
        ScriptManager.RegisterClientScriptBlock(updtPnl, this.GetType(), "alertMessage", "alert('" + error + "')", true);
        ScriptManager.RegisterStartupScript(updtPnl, this.GetType(), "alertscipt", "swal('Oops!','Someting Went Wrong While Saving Official Information !!!','warning')", true);
        return;
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on May 30, 2022 05:07 AM

Hi thereallover0...,

You need to set the GridView Control ID as ControlID for PostBackTrigger.

Refer modified code.

HTML

<asp:UpdatePanel ID="updtPnl" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ID="gvEducation" runat="server"
            Font-Size="Small"
            EmptyDataText="No records Found"
            AutoGenerateColumns="False"
            CssClass="table table-striped table-bordered table-hover table-responsive table-colored table-info"
            OnRowDataBound="gvEducation_RowDataBound">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="S.No" HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <%# Container.DataItemIndex + 1 %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="schoolCollege" HeaderText="Organization" />
                <asp:BoundField DataField="degree" HeaderText="Degree" />
                <asp:BoundField DataField="IMAGe_NAMe" HeaderText="Name" />
                <asp:BoundField DataField="EXT" HeaderText="EXT" />
                <asp:TemplateField HeaderText="Document">
                    <ItemTemplate>
                        <asp:Image ID="ImageActual" runat="server" Style="height: 50px; width: 50px; border-style: ridge; border-width: 5px;" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Document">
                    <ItemTemplate>
                        <asp:LinkButton ID="DocumentEducation" CssClass="btn btn-rounded btn-info" CommandArgument='<%# Eval("SNO") %>' Text="Download" runat="server" OnClick="DocumentEducation_Click"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
 
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
 
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="gvEducation" />
    </Triggers>
</asp:UpdatePanel>

Code

protected void DocumentEducation_Click(object sender, EventArgs e)
{
    try
    {
        LinkButton lnk = (LinkButton)sender;
        GridViewRow gr = lnk.NamingContainer as GridViewRow;
        int SNO = int.Parse((sender as LinkButton).CommandArgument);
        DataTable dt = blun.Education(SNO);
        string ext = dt.Rows[0]["ext"].ToString();
        string[] extsss = ext.Split('.');
        string last = extsss[extsss.Length - 1];

        Response.Clear();
        Response.BufferOutput = false;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = ContentType;
        Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["Image_Name"].ToString());
        Response.BinaryWrite((byte[])dt.Rows[0]["Image"]);
        Response.Flush();
    }
    catch (Exception ex)
    {
        string error = ex.ToString();
        ScriptManager.RegisterClientScriptBlock(updtPnl, this.GetType(), "alertMessage", "alert('" + error + "')", true);
        ScriptManager.RegisterStartupScript(updtPnl, this.GetType(), "alertscipt", "swal('Oops!','Someting Went Wrong While Saving Official Information !!!','warning')", true);
        return;
    }
    finally
    {
        Response.End();
    }
}