Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In gridview There is an image button to download file. I have used the gridview with updatepanel.If I removed Updatepanel the download option will work fine.But i have to use Updatepanel in this.How can Isolve this?
XML
<asp:UpdatePanel ID="UpdatePanel2" runat="server" >
                       <ContentTemplate>
              <asp:GridView ID="GridImport" runat="server" AutoGenerateColumns="false" Width="99%" Height="50%"
                                   AllowPaging="true" GridLines="None" Style="padding: 15px; text-align: left; overflow: scroll;
                                   font-family: Arial; font-size: 11pt;" ShowHeaderWhenEmpty="true" PageSize="5"
                                   CssClass="Grid_LE" HeaderStyle-CssClass="Grid_Head" EmptyDataText = "No files Imported" OnPageIndexChanging="GridImport_PageIndexChanging">
               <Columns>
                   <asp:BoundField DataField="Text" HeaderText="File Name" />
                   <asp:TemplateField>
                       <ItemTemplate>
                             <asp:ImageButton ID="lnkDownload" runat="server" CommandArgument='<%# Eval("Value") %>'  Style="width: 20px; height: 20px;" ImageUrl="~/Images/download.png" OnClick = "DownloadFile"  />   <%--CommandName="Upload"  OnClick="DownloadFile"--%>
                       </ItemTemplate>
                   </asp:TemplateField>
                   <asp:TemplateField>
                       <ItemTemplate>
                             <asp:ImageButton ID="lnkDelete" runat="server" CommandArgument='<%# Eval("Value") %>'  Style="width: 20px; height: 20px;" ImageUrl="~/Images/cancel.png" OnClick="DeleteFile" />
                       </ItemTemplate>
                   </asp:TemplateField>
               </Columns>
           </asp:GridView>
          </ContentTemplate>
                   </asp:UpdatePanel>




C#
.cs

   protected void DownloadFile(object sender, EventArgs e)
        {
            string filePath = (sender as ImageButton).CommandArgument;
            Response.ContentType = ContentType;
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
            Response.WriteFile(filePath);
            Response.End();
        }
        protected void DeleteFile(object sender, EventArgs e)
        {
            string filePath = (sender as ImageButton).CommandArgument;
            File.Delete(filePath);
            ExprotFileList();
            ClientScript.RegisterStartupScript(GetType(), "Val", "ShowExpPopup();", true);
        }
Posted
Updated 15-Sep-16 23:31pm

Hi,

For Download action it requires a post back action, but in update panel post back action does not fires..
so you need to add trigger for the control to initiate download..


<asp:updatepanel runat="server" id="upPanel1" xmlns:asp="#unknown">
<contenttemplate>
//Contents
</contenttemplate>
<trigger>
<asp:postbacktrigger controlid="ID" />
</trigger>
</asp:updatepanel>
 
Share this answer
 
Comments
jithesh a 7-Nov-14 3:40am    
<asp:postbacktrigger controlid="ID" /> .Is this ID for 'lnkDownload' in above code?
Magesh M N 9-Nov-14 22:17pm    
yes
jithesh a 9-Nov-14 23:02pm    
Its not working
it is working in updatepanel


ASP.NET
</ContentTemplate>
  <Triggers>
        <asp:PostBackTrigger ControlID="GridView1" />
    </Triggers>
 </asp:UpdatePanel>
 
Share this answer
 
Add a button Outside of updatepanel
<asp:button id="BtnDownload" onclick="DownloadFile_Click" runat="server" visible="false" xmlns:asp="#unknown" />


Javascript function

XML
function DownloadFile(filepath) {
           __doPostBack("<%= BtnDownload.UniqueID %>", filepath);
       }


XML
<asp:UpdatePanel ID="UpdatePanel2" runat="server" >
                       <ContentTemplate>
              <asp:GridView ID="GridImport" runat="server" AutoGenerateColumns="false" Width="99%" Height="50%"
                                   AllowPaging="true" GridLines="None" Style="padding: 15px; text-align: left; overflow: scroll;
                                   font-family: Arial; font-size: 11pt;" ShowHeaderWhenEmpty="true" PageSize="4"
                                   CssClass="Grid_LE" HeaderStyle-CssClass="Grid_Head" EmptyDataText = "No files Imported" OnPageIndexChanging="GridImport_PageIndexChanging"  >
               <Columns>
                   <asp:BoundField DataField="Text" HeaderText="File Name" />
                   <asp:TemplateField>

                       <ItemTemplate>
                             <asp:ImageButton ID="lnkDownload" runat="server" CommandArgument='<%# Eval("Value") %>'  Style="width: 20px; height: 20px;" ImageUrl="~/Images/download.png"  OnClick = "lnkDownloadNew_Click" />    <%--OnClick = "DownloadFile"  --%>
                       </ItemTemplate>
                   </asp:TemplateField>
                   <asp:TemplateField>
                       <ItemTemplate>
                             <asp:ImageButton ID="lnkDelete" runat="server" CommandArgument='<%# Eval("Value") %>'  Style="width: 20px; height: 20px;" ImageUrl="~/Images/cancel.png" OnClick="DeleteFile" />
                       </ItemTemplate>
                   </asp:TemplateField>
               </Columns>
           </asp:GridView>
       </ContentTemplate>

            </asp:UpdatePanel>



.cs

C#
protected void DownloadFile_Click(object sender, EventArgs e)
     {
         //var  filePath = Request.Form["__EVENTARGUMENT"];
         string filePath = ViewState["filepath"].ToString();
         Response.ContentType = ContentType;
         Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
         Response.WriteFile(filePath);
         Response.End();
         ExprotFileList();
     }
     protected void lnkDownloadNew_Click(object sender, EventArgs e)
     {
         string filePath = (sender as ImageButton).CommandArgument;
         ScriptManager.RegisterStartupScript(this, typeof(ImageButton), "scr", "DownloadFile('" + filePath + "');", true);
         ViewState["filepath"] = filePath;

     }
 
Share this answer
 
Comments
Tahir Mahmood From karachi 16-Sep-16 5:29am    
it is tested by me and and it is working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900