zoukankan      html  css  js  c++  java
  • 解决ASP.NET应用AJAX的CascadingDropDown回发或回调参数无效

    解决ASP.NET应用AJAX的CascadingDropDown回发或回调参数无效
    本例子是级联下拉效果,出现了“回发或回调参数无效”问题,结合网上的介绍和自己的测试,下面就讲讲:
    页面aspx代码
     <tr>
                    <td align="right" style="height: 50px; 300px;">
                        系别
                    </td>
                    <td align="left" style="height: 50px">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:DropDownList ID="Department" runat="server">
                        </asp:DropDownList>
                        <cc1:CascadingDropDown ID="CascadingDropDown1" runat="server" Category="province"
                            LoadingText="正在加载..." PromptText="请选择系别" ServiceMethod="GetDropDownContents" TargetControlID="Department"
                            ServicePath="WebService.asmx">
                        </cc1:CascadingDropDown>
                    </td>
                </tr>
                <tr>
                    <td align="right" style="height: 50px; 300px;">
                        班级
                    </td>
                    <td align="left" style="height: 50px">
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <asp:DropDownList ID="Class" runat="server" OnSelectedIndexChanged="Class_SelectedIndexChanged"
                            AutoPostBack="true">
                        </asp:DropDownList>
                        &nbsp;
                        <cc1:CascadingDropDown ID="CascadingDropDown2" runat="server" Category="city" LoadingText="正在加载..."
                            PromptText="请选择班级" ServiceMethod="GetDropDownContents" TargetControlID="Class"
                            ServicePath="WebService.asmx" ParentControlID="Department">
                        </cc1:CascadingDropDown>
                    </td>
                </tr>
    可能出现的问题:
    回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
    英文描述
    id 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 valida

    可行的解决方法有:

    1、在页面的<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 中添加 EnableEventValidation="false" 就可以了。(首先考虑的)

    2、通过web.config
    <system.web>
    <pages enableEventValidation="false"/>

    3、是Form嵌套,一个页面只能有一个Form,仔细检查代码就可以解决。

    4、如果页面含有 DropDownList 或 ListBox这样的控件,可能以下原因造成:

        4.1  在下拉菜单中使用ajax,常见于省市联动菜单,可能是由于在aspx页面赋给了下拉菜单初始Item值,在事件回发时提示该错误,将下拉菜单初始Item值删除,在绑定事件中添加Item项。

        4.2  原因是 DropDownList 控件的ListItem 的Value 属性 包含汉字.只要将Value 改为英文或数字的就行了.最好在web.config中添加如下语句:
    <globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="zh-CN" uiCulture="zh-CN"/>因为 POSTBACK 如果不采用 UTF-8 编码, JAVASCRIPT 会认为有问题。
    只改 requestEncoding="utf-8" 就可以了,responseEncoding="utf-8" 不用

    5.Register For Event Validation
    其原理就是让asp.net记录这个postback value.
    RegisterForEventValidation必须在render时调用.

    protected override void Render(HtmlTextWriter writer)
    {
    ClientScript.RegisterForEventValidation(_recipeList.UniqueID,"4");
    base.Render(writer);
    }

    如果我们自己写了一个control,需要使用validate events功能,就需要使用SupportsEventValidation attribute,

    [SupportsEventValidation]
    public class DynamicDropDownList : DropDownList
    {
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
    Page.ClientScript.RegisterForEventValidation(this.UniqueID, "4");
    base.Render(writer);
    }
    }

  • 相关阅读:
    utf8编码和中文不能解码问题解决
    python环境的安装配置
    repo同一个仓的同一个changeId的提交
    Jenkins pipeline之将命令的运行结果赋值给变量
    repo和git常用的命令和场景
    docker 安装rabbitmq
    docker的一些概念
    mysql数据库sql优化原则
    数据库优化02
    MySQL数据库优化总结
  • 原文地址:https://www.cnblogs.com/hakuci/p/1150945.html
Copyright © 2011-2022 走看看