EventValidation,网格中命令按钮上的无效回发或回调参数

时间:2019-05-24 13:21:58

标签: c# asp.net events postback eventvalidation

我在网格中有一个命令按钮。
我遇到错误:

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

基于我对 About Asp.Net DataList Commands And EventValidation

似乎如果在ASP控件中指定了事件方法,那么我应该不会收到此错误。该帖子上的解决方案指示将事件规范移至ASP控制标签定义中。

但是我的代码没有这个问题,仍然出现这种类型的错误,因为我通过以下方式在asp控件定义本身中指定了事件:OnRowCommand =“ GridView1_OnRowCommand”

如何在不禁用页面级EventValidation的情况下解决?

我的代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ProductMaint.aspx.cs" Inherits="Questpond.ProductMaint" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">



<head runat="server">
</head>

<body style="width: 749px; height: 336px;">
    <form id="form1" runat="server">

        <br />

            <asp:GridView ID="grdProducts" runat="server" Width="754px" AutoGenerateColumns="False" Height="40px"  >
            <Columns>




                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="Edit"
                            Text="Edit" OnRowCommand="GridView1_OnRowCommand" CommandArgument='<%# Eval("RW") %>' Value='<%# Eval("RW") %>' />
                    </ItemTemplate>
                </asp:TemplateField>




                <asp:TemplateField HeaderText="Inactive">
                <ItemTemplate>
                <asp:Checkbox ID="Checkbox1" runat="server" CausesValidation="false" CommandName="chkInactive"  Checked='<%# Eval("Inactive")%>'    
                     Enabled="false"/>
                </ItemTemplate>
                </asp:TemplateField>




                <asp:BoundField DataField="Id" HeaderText="Id" />
                <asp:BoundField DataField="Category" HeaderText="Category" />
                <asp:BoundField DataField="Description" HeaderText="Description" />
                <asp:BoundField DataField="CurrentPrice" HeaderText="Price" />

            </Columns>



            </asp:GridView>



        <asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IQBooksBusinessLayerSalesData;

namespace Questpond
{
    public partial class ProductMaint : System.Web.UI.Page
    {

        protected void RefreshGridWithData()
        {      
            DataSet ds;

            //works fine if default on screen is nothing and set to exclude deleted rows.
            ds = IQBooksBusinessLayerSalesData.IQBooksBusinessLayerSalesDataAccess.GetProducts();
            this.grdProducts.DataSource = ds;
            this.grdProducts.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (this.IsPostBack)
            //{
            //}
            //else
            //{
            //    this.RefreshGridWithData();  
            //}

            this.RefreshGridWithData();
        }


        protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName != "Edit") return;
            int id = Convert.ToInt32(e.CommandArgument);
            lblTest.Text = id.ToString();
            // do something
        }
    }
}

0 个答案:

没有答案