解決ASP.NET應用AJAX的CascadingDropDown回傳或回調參數無效

來源:互聯網
上載者:User
解決ASP.NET應用AJAX的CascadingDropDown回傳或回調參數無效
本例子是級聯下拉效果,出現了“回傳或回調參數無效”問題,結合網上的介紹和自己的測試,下面就講講:
頁面aspx代碼
 <tr>
                <td align="right" style="height: 50px; width: 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; width: 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);
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.