UpdatePanel中的下载文件(图像按钮)选项不起作用?

时间:2014-11-07 08:13:01

标签: asp.net

在gridview中有一个图像按钮可下载文件。我已将gridview与updatepanel一起使用。如果我删除了Updatepanel,下载选项将正常工作。但我必须在此使用Updatepanel。如何解决这个问题?

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

2 个答案:

答案 0 :(得分:2)

在您的代码文件中:

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

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

在您的标记文件中,在更新面板外添加此按钮:

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

Javascript代码:

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

答案 1 :(得分:0)

在代码中使用trigger

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