【问题标题】:Issue while clicking on ImageButton单击 ImageButton 时出现问题
【发布时间】:2014-11-07 06:39:00
【问题描述】:

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

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

我该如何解决?

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    在 Page_Load() 中绑定你的 gridview

    if (!IsPostBack)
    {
        //Your code for Bind data 
    }
    

    欲了解更多信息,请访问: Error: Invalid postback or callback argument

    【解决方案2】:

    回发或回调参数无效。使用配置或页面中的 启用事件验证。出于安全目的,此功能验证回发或回调事件的参数是否源自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用 ClientScriptManager.RegisterForEventValidation 方法注册回发或回调数据以进行验证。

    解决方法如下:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多