单击ImageButton时出现问题

时间:2014-11-07 06:39:01

标签: asp.net

在网格中有一个图像按钮来下载文件。单击此图像按钮时出现错误。

 <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">
                   <Columns>
                       <asp:BoundField DataField="Text" HeaderText="File Name" />
                       <asp:TemplateField>
                           <ItemTemplate>
                            <%--   <asp:LinkButton ID="lnkDownload" Text = "Download" CommandArgument = '<%# Eval("Value") %>' runat="server" OnClick = "DownloadFile" ></asp:LinkButton> --%>
                                 <asp:ImageButton ID="lnkDownload" runat="server" CommandArgument='<%# Eval("Value") %>'  Style="width: 24px; height: 24px;" ImageUrl="~/Images/download.png" OnClick="DownloadFile" />   <%--CommandName="Upload"--%>
                           </ItemTemplate>
                       </asp:TemplateField>
                       <asp:TemplateField>
                           <ItemTemplate>
                               <%--<asp:LinkButton ID = "lnkDelete" Text = "Delete" CommandArgument = '<%# Eval("Value") %>' runat = "server" OnClick = "DeleteFile" /> --%>
                                 <asp:ImageButton ID="lnkDelete" runat="server" CommandArgument='<%# Eval("Value") %>'  Style="width: 24px; height: 24px;" ImageUrl="~/Images/cancel.png" OnClick="DeleteFile" />
                           </ItemTemplate>
                       </asp:TemplateField>
                   </Columns>
               </asp:GridView>

这是我的DownloadFile功能

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

点击下载按钮时我得到了这个错误

Server Error in '/' Application.

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

我如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

在Page_Load()

中绑定gridview
if (!IsPostBack)
{
    //Your code for Bind data 
}

有关更多信息,请访问: Error: Invalid postback or callback argument

答案 1 :(得分:1)

无效的回发或回调参数。使用配置或&lt;%@ Page EnableEventValidation =“true”%&gt;启用事件验证在一个页面中。出于安全考虑,此功能可验证回发或回调事件的参数是否来自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用ClientScriptManager.RegisterForEventValidation方法注册回发或回调数据以进行验证。

以下是解决方案:

<%@ Page Title="" Language="C#" 
EnableEventValidation="false" %>