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);
    }
    }

  • 相关阅读:
    应对需求变更的软件的设计——我的想法
    重温数据库访问——故事篇
    用接口实现事件的一种方法,只是玩玩。
    面向对象最重要的是“抽象”,三层最重要的也是“抽象”,没有抽象就不是真正的面向对象、三层。
    【视频】自然框架之分页控件的使用方法(一) PostBack方式的一般分页方式
    【自然框架 免费视频】资源角色的思路介绍(整理了一下以前帖子的目录,请刷新)
    自信。
    钢铁是怎样炼成的?千锤百炼
    Android 中文API (94) —— MediaController
    android中文api(80)——Gallery.LayoutParams
  • 原文地址:https://www.cnblogs.com/hakuci/p/1150945.html
Copyright © 2011-2022 走看看