2010-08-03 92 views
5

我有一個網頁與各種控制。其中兩個是下拉列表。第一個下拉列表將從page_load事件的xml文件中填充。這工作正常。在第一個下拉列表中,附加了一個級聯下拉列表擴展器,每當第一個下拉列表中的選擇發生更改時,就會調用Web服務。這工作也很好。在我的兩個dropdownlists下面,我有一個按鈕,可以將頁面返回。然而,當我在第二個下拉列表已經做出了選擇,並點擊按鈕,我得到以下錯誤:回發或回調參數無效。當點擊按鈕

Server Error in '/' Application. Invalid postback or callback argument. Event validation is enabled using 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 validation. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Invalid postback or callback argument. Event validation is enabled using 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 validation.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ArgumentException: Invalid postback or callback argument. Event validation is enabled using 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 validation.] System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +10945696 System.Web.UI.WebControls.DropDownList.LoadPostData(String postDataKey, NameValueCollection postCollection) +72 System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +507 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2071

對不起,可怕的格式。有關爲何引發此錯誤以及如何防止它的任何建議?

感謝,

回答

3

我剛剛找到了自己的問題的答案。

問題是,AJAX將新值添加到下拉列表中,但因爲它們不在視圖狀態中,ASP.NET因出錯而停止。這裏有一篇很棒的博客文章,解釋瞭如何解決這個問題,它對我很好。

基本上你只是子類的DropDownList類擺脫了SupportsEventValidation屬性 - > ASP.NET不驗證值,一切運行良好!

這裏閱讀整篇帖子: Subclassing the DropDownList to remove the SupportsEventValidation attribute

+0

一個漂亮的解決方法,但仍然hacky國際海事組織。不要試圖'破解'已經很爛的東西(即CDDL)。還是你的電話。 =) – RPM1984 2010-08-03 03:49:43

+0

我看了看更新面板,但卡住了。我想將第二個下拉列表的文本更改爲「正在加載...」,而後臺邏輯正在獲取這些值。我正在使用附加到第一個下拉列表的onchange javascript。但是,文本更改正常,但回發似乎中斷,沒有值加載。任何想法如何解決這個問題? – b3n 2010-08-03 05:20:13

+0

我正在做同樣的事情。我有一個下拉菜單:選擇狀態。當他們選擇一個狀態時,第二個下拉列表將文本設置爲「加載城市...」,對Web服務進行ajax調用,並將其綁定到下拉列表。全部使用jQuery和Web服務調用。 (第一次ddl的onchange事件)。要在後臺執行此操作,您需要異步調用Web服務,並提供回調函數 - 然後在回調函數中清除加載文本並綁定值。 – RPM1984 2010-08-03 05:57:55

3

這是一個已知的問題AJAX CascadingDropDown擴展。

爲了使其正常工作,您需要禁用事件驗證。

這裏有一個線程討論這個問題: http://forums.asp.net/t/1032053.aspx

我有同樣的問題,這就是爲什麼我拋棄使用CascadingDropDown擴展,只是使用普通的客戶端下拉列表的一些jQuery的。

你有兩個選擇:

  1. 溝AJAX CascadingDropDown,定期更換下拉菜單,在客戶端點擊使用jQuery/JavaScript的調用Web服務。
  2. 禁用頁面上的事件驗證。 (不建議)。

事件驗證可防止頁面狀態在請求之間被篡改。不幸的是,無論出於何種原因,AJAX CDDL都會這樣做。

爲了讓CDDL能夠正常工作,這不是你應該禁用的東西,因爲它會影響整個頁面並可能導致安全問題。

我的建議,咬緊牙關 - 溝CDDL和替換爲jQuery。

+0

嗨RPM,謝謝你的回覆。檢查我上面的帖子中的鏈接,這很好,你不必關閉頁面上的事件驗證。 – b3n 2010-08-03 03:47:57

0

我已經搜查了很多,結束了以下解決方法: http://avinashsing.sunkur.com/2011/03/24/dropdownlist-in-asp-net-does-not-retain-control-state/

對於一般用戶,在你的父頁:

Protected Sub cbGenericDropDownList_DataBound(sender As Object, e As System.EventArgs) If Not IsNothing(HttpContext.Current.Request(DirectCast(sender, DropDownList).UniqueID)) Then DirectCast(sender, DropDownList).SelectedValue = _ HttpContext.Current.Request(DirectCast(sender, DropDownList).UniqueID) End If End Sub

然後爲您的頁面中的每個DropDownList控件添加: OnDataBound =「cbGenericDropDownList_DataBound」

相關問題