2011-05-09 31 views
7

我最近開始使用ScriptManager。我有一個ASP.NET DropDownList控件,我通過JavaScript填充。不過,我正在使用事件驗證。所以我遇到下面的錯誤,如果我不使用「RegisterForEventValidation」調用我的下拉列表。我如何知道在第二個參數(我有「價值」)中設置了什麼值?我通過JavaScript填充我的下拉列表,所以我不知道我的代碼背後有什麼值。我猜測渲染是在AJAX局部渲染回發期間調用的,對嗎?或者不是,所以無論我是否做整頁回發,都會調用它。我想我不僅想聽到我的問題的答案,但如果你可以與我分享你的經驗,關於下面的錯誤。我喜歡輸入,就像約翰尼#5一樣。應如何正確使用RegisterForEventValidation

==================

背後代碼:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter) 

    Page.ClientScript.RegisterForEventValidation(DDLTest.UniqueID, "value") 
    MyBase.Render(writer) 
End Sub 

============== ====

錯誤:

Server Error in '/' Application. 
Invalid 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 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 <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 validation. 

回答

10

從閱讀腳本管理和反覆試驗經過廣泛的研究,這裏是我發現了什麼。

您可以禁用事件驗證,但這並不總是最好的選擇,因爲同時具有JavaScript驗證和ASP.NET驗證是一種很好的做法。這是在您的下拉列表中註冊這些值的全部目的。如果您有將選項插入到選擇內的JavaScript(從ASP.NET DropDownList控件中選擇呈現),則只要可能,您都希望阻止腳本注入。

回答我的問題:爲此,我們將這個RegisterForEventValidation稱爲每個可能的值,它可以出現在您的應用程序中任何可能出現的下拉列表中。就我而言,我有兩個下拉菜單。一個下拉菜單用於導致回發,並根據第一個下拉列表重新填充第二個下拉列表。但是,現在我正在使用JavaScript將值注入到jQuery的下拉列表中。

在重新填充值之前,我使用jQuery刪除所有值。

jQuery("#<%=DDLTest.ClientID %>").children("option").each(function() { 
    jQuery(this).remove(); 
}); 

當我的第一個下拉更改時,我用與第一個下拉值相關的值重新填充第二個下拉列表。

var map = { 
       "1112": "Hair 5 Drug Panel", 
       "1121": "Hair 5 Drug Panel and Extended Opiates Limit of Detection Test", 
       "1120": "Hair 5 Drug Panel Limit of Detection Test" 
      }; 

var thisTemp = this; // the reason I do this is because "this" is already being used in the callback. 

jQuery.each(map, function(key, val) { 
    jQuery(thisTemp.Elements.DDLTest).append(jQuery("<option></option>").val(key).text(val)); 
}); 

並選擇我需要的值。

jQuery(this.Elements.DDLTest).val(quickDataEntryObject.TestPricingOptionId); 

然而,所有這個JavaScript代碼發生之前,我註冊了下拉的可能值。您必須在Render事件中執行此操作。

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter) 

     Dim testPricingOptionTable As DataTable = ApplicationContext.Database.ExecuteDataSet("procEventValidationRegisteredValues", "test_pricing_options").Tables(0) 

     For Each testPricingOptionRow As DataRow In testPricingOptionTable.Rows 
      Page.ClientScript.RegisterForEventValidation(DDLTest.UniqueID, testPricingOptionRow(0).ToString) 
     Next 
     MyBase.Render(writer) 

    End Sub 
+0

@MacGuyver,錯了,您不必在Render()方法中調用RegisterForEventValidation。您可以在調用Render()方法期間或之前調用RegisterForEventValidation。 – Bradley 2013-03-28 14:04:30

+3

@ user2220060感謝您澄清。自從我看了這一年已經過去了一年多了。只是爲了幫助其他讀者,你能列出你可以從中調用的其他方法嗎?我記得ASP.NET在整頁回發期間有一系列事件觸發,但我不記得它們是什麼。 – MacGyver 2013-07-16 21:19:39

+0

@Bradley你確定嗎?我試圖在預渲染期間運行它,並得到錯誤消息:「System.InvalidOperationException:'RegisterForEventValidation只能在Render();''中調用 – 2017-06-08 18:56:11

2

對於事件驗證工作性質,你必須註冊使用RegisterForEventValidation控制每一個可能的崗位價值。您可以多次調用此方法爲同一控件註冊多個值。

有時,這是不可能的 - 例如,在您從Java腳本動態填充下拉列表的情況下。解決方案(S)將

  1. 整個頁面
  2. 禁用事件驗證使用一些巧妙的技巧 - 例如,註冊事件驗證這最好是在下拉列表中總是存在的(一些值,例如「 - - 選擇 - 「),在提交時,將選定的下拉值放入隱藏字段,並將下拉選擇值設置爲我們用於註冊的值。如果不存在,則可能需要將該值添加到下拉列表中。
  3. 編寫您自己的自定義控件,不參與事件驗證或根據您的需求進行。

我一般用1號或3去基於需求/預算等

+0

感謝您的回答。我昨天自己解決了這個問題。對於那些閱讀,請閱讀下一篇文章,瞭解我在這篇文章中的想法,以便澄清Vinay從廣泛研究中得到的答案。 Vinay,我會給你一點! – MacGyver 2011-05-10 16:22:15