无效的回发或回调参数。设置了AutoPostBack,并在Page_Load

时间:2017-11-22 17:08:14

标签: c# asp.net

当我点击ListBox中的任何项目时出现以下错误:

  

无效的回发或回调参数。事件验证已启用   在配置中使用或<%@   Page EnableEventValidation =" true" %GT;在一个页面中。为了安全   目的,此功能验证回发或回调的参数   事件源自最初呈现的服务器控件   他们。如果数据有效且预期,请使用   ClientScriptManager.RegisterForEventValidation方法   注册回发或回调数据以进行验证。

但是,我设置了AutoPostBack =" True"在我的ListBox和我的DropDownList中:

<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true"  CodeBehind="Form1.aspx.cs" Inherits="WebApplication1.Form1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="dropDown" runat="server" AutoPostBack="True" Height="19px" Width="226px">
            </asp:DropDownList>
        </div>
        <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Height="349px" Width="501px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" ></asp:ListBox>
        <br />
    </form>
</body>
</html>

我在我的.cs代码中设置if(!Page.IsPostBack):

using System;
using System.Collections.Generic;
using System.Web.UI;

namespace WebApplication1
{
    public partial class Form1 : System.Web.UI.Page
    {
        public ServiceReference1.WebService1SoapClient client= new ServiceReference1.WebService1SoapClient();
        public localhost.WebService1 asd = new localhost.WebService1();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                 List<string> igralci = new List<string>();
                 foreach (localhost.Igralec ig in asd.vrniVseIgralce())
                 {
                     igralci.Add(ig.id + ", " + ig.ime + ", " + ig.priimek + ", " + ig.letoRojstva + "\n");
                 }
                 ListBox1.DataSource = igralci;
                 ListBox1.DataBind();


                List<string> klubi = new List<string>();
                klubi.Add("Vsi klubi");
                foreach (localhost.Klub ig in asd.vrniVseKlube())
                {
                    klubi.Add(ig.naziv);
                }

                dropDown.DataSource = klubi;
                dropDown.DataBind();

            }
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string script = "alert(\"Hello!\");";
            ScriptManager.RegisterStartupScript(this, GetType(),
                                  "ServerControlScript", script, true);
        }

    }
}

任何人都知道如何摆脱错误?

1 个答案:

答案 0 :(得分:0)

它自己说错误你需要添加&#34;&lt;%@ Page EnableEventValidation =&#34; true&#34; %GT; &#34;在页面中,如下所示: -

&#34; &lt;%@ Page Language =&#34; C#&#34; AutoEventWireup =&#34;假&#34; EnableEventValidation =&#34;假&#34;代码隐藏=&#34; Form1.aspx.cs&#34;继承=&#34; WebApplication1.Form1&#34; %GT;&#34;

并且错误被整理出来。