download file (Image Button ) option is no

2020-05-07 07:01发布

问题:

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?

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

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

回答1:

In your code file:

protected void ClickFromUpdate_Click(object sender, EventArgs e)
{
      string filePath = (sender as ImageButton).CommandArgument;
      ScriptManager.RegisterStartupScript(this, this.GetType(), "DownloadScript", "$(function() {DownloadFile(" + filePath + ");});", true);
}

protected void DownloadFile_Click(object sender, EventArgs e)
{
     //Your download code
     var filePath = Request.Form["__EVENTARGUMENT"];
     Response.ContentType = ContentType;
     Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
     Response.WriteFile(filePath);
     Response.End();
}

In your markup file inside the updatepanel:

<asp:TemplateField>
    <ItemTemplate>
        <asp:ImageButton ID="lnkDownload" runat="server" CommandArgument='<%# Eval("Value") %>'  Style="width: 20px; height: 20px;" ImageUrl="~/Images/download.png" OnClick = "ClickFromUpdate_Click"  />
    </ItemTemplate>
</asp:TemplateField>

In your markup file add this button outside your updatepanel:

<asp:Button ID="ButtonDownload"  OnClick="DownloadFile_Click" runat="server" />

Javascript code:

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


回答2:

use trigger in your code

 <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>
       <Triggers>
            <asp:PostBackTrigger ControlID="lnkDownload" /> **Or**
            <asp:AsyncPostBackTrigger ControlID="lnkDownload" EventName="OnClick" />
        </Triggers>
                    </asp:UpdatePanel>


标签: asp.net