vs2005下,"回发或回调参数无效"的解决方法

还是前几天发现的问题,原来在vs2003下使用的ajax无刷新联动代码,放到vs2005的项目后就会出现错误
回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
在网上找到几种解决方案
1.将<pages enableEventValidation="true"/>改为<pages enableEventValidation="false"/>,但是这样取不到客户端增加的值。
2.将dropdownlist的value值设为数字或者英文,不要使用中文。我尝试了一下,没有成功。
3.使用RegisterForEventValidation注册客户端控件的值。
可能还有别的方法,不过我没有注意到。我是参考的第三种方法实现的。

解决方法:在Page_Load之前加入如下代码
protected override void Render(HtmlTextWriter writer)
    {
        ClientScript.RegisterForEventValidation(DropDownList1.UniqueID, "argument");
        base.Render(writer);
    }
DropDownList1就是你要使用客户端脚本动态增加内容的控件,而argument就是你要加入的值。例如本来DropDownList1是空的,你用脚本为它增加了“中国”,“美国”等值,这里的代码就改成
ClientScript.RegisterForEventValidation(DropDownList1.UniqueID, "中国");
ClientScript.RegisterForEventValidation(DropDownList1.UniqueID, "美国");
原理:就是因为vs2005的这个数据验证问题了,"在asp.net render DropDownList 时,会遍历DropDownList的item,并记录所有可能的postback的值,计算的结果会被保存在page中,
<input type="hidden"
       name="__EVENTVALIDATION"
       id="__EVENTVALIDATION"
       value="/wEWBQKGg9abDQKd9sHMBgKc9s…….."
/>
这个过程发生在control的Render()方法中
当页面postback时,ASP.NET会根据这个隐藏值检查postback values,如果找不到对应信息,就会报错所以我们重写Render,将由脚本加入控件中的值加入到隐藏值中,就不会出现找不到相应信息的错误了。使用这个方法后,取数据时使用Request.Form["DropDownList2"]取值.

如何通过异步回调根据其他 DropDownList 控件值更新 DropDownListWeb 服务器控件值
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpdateDropDownboxBasedOnAnotherDropDownBox.aspx.cs" Inherits="UpdateDropDownboxBasedOnAnotherListbox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Asynchronous Callback - DropDownList</title>
    <script language="javascript">
        function ReceiveServerData(arg,context)
        {
        var drpEmployeeName = document.forms[0].elements['drpEmployeeName'];
        drpEmployeeName.innerHTML= "";
        var rows = arg.split('||');
        for (var i = 0; i < rows.length - 1; ++i)
        {
         var fields = rows[i].split('|');
         var firstname = fields[0];
         var employeeid = fields[1];
         var option = document.createElement("option");
         option.value = employeeid;
         option.innerHTML = firstname;
         drpEmployeeName.appendChild(option);
         document.getElementById('lblMessage').innerText = "You selected employeeid: " + employeeid + " for which FirstName is " + firstname ;
        }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="drpEmployeeId" runat="server" DataSourceID="SqlDataSource1"
            DataTextField="employeeid" DataValueField="employeeid" Style="z-index: 100; left: 144px;
            position: absolute; top: 112px">
        </asp:DropDownList>
        <asp:DropDownList ID="drpEmployeeName" runat="server" Style="z-index: 101; left: 144px;
            position: absolute; top: 136px">
        </asp:DropDownList>
        <asp:Label ID="lblEmployeeId" runat="server" Style="z-index: 102; left: 48px; position: absolute;
            top: 112px" Text="EmployeeID:" Width="88px"></asp:Label>
        <asp:Label ID="lblFirstName" runat="server" Style="z-index: 103; left: 48px; position: absolute;
            top: 136px" Text="First Name" Width="88px"></asp:Label>
        <asp:Label ID="lblText" runat="server" Font-Names="Arial" Font-Size="Small" Style="z-index: 104;
            left: 48px; position: absolute; top: 80px" Text="Select EmployeeId from first DropDown and Second DropDown Will Be Updated Asynchronously"
            Width="600px" Font-Underline="True"></asp:Label>
        <asp:Label ID="lblTitle" runat="server" Font-Bold="True" Font-Names="Verdana" Style="z-index: 105;
            left: 24px; position: absolute; top: 24px" Text="Updating DropDownListBox Control Asynchronously"
            Width="488px"></asp:Label>
    </div>
    <asp:Label ID="lblMessage" runat="server" Style="z-index: 107; left: 48px; position: absolute;
    top: 192px" Width="592px" Font-Names="Arial" Font-Size="Small" ForeColor="MediumBlue"></asp:Label>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [employeeid], [firstname] FROM [Employees]">
    </asp:SqlDataSource>
    </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Text;
using System.Data.SqlClient;
public partial class UpdateDropDownboxBasedOnAnotherListbox : System.Web.UI.Page, ICallbackEventHandler
{
    StringBuilder arg;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Do not process Page_Load if it is a clientscript callback.
        if (IsCallback)
            return;
        if (!Page.IsPostBack)
        {
            //Generate an HTML script that can instantiate an asynchronous callback on the click of
            //the first DropDownListBox, and add it to the "onClick" attribute of the same.
            string FirstDropDownCallbackScript = Page.ClientScript.GetCallbackEventReference(this, "document.all['drpEmployeeId'].value", "ReceiveServerData", "null");
            drpEmployeeId.Attributes["onClick"] = FirstDropDownCallbackScript;  
        }
    }
    // *******************************************************
    // Implement the callback interface
    public void RaiseCallbackEvent(String eventArgument)
    {
        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Select firstname,employeeid from Employees where employeeid= @EmployeeId";
            cmd.Parameters.Add(new SqlParameter("@EmployeeId", SqlDbType.Int, 4));
            cmd.Parameters["@EmployeeId"].Value = Int32.Parse(eventArgument);
            cmd.Connection = con;
            arg = new StringBuilder();
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                arg.Append(reader["firstname"]);
                arg.Append("|");
                arg.Append(reader["employeeid"]);
                arg.Append("||");
            }
            reader.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
    public string GetCallbackResult()
    {
        return arg.ToString() ;
    }
    // *******************************************************
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhangjie_xiaoke/archive/2008/11/24/3363786.aspx

原文地址:https://www.cnblogs.com/googlegis/p/2979023.html