RegisterForEventValidation 應該如何正確使用
我最近開始使用 ScriptManager。我有一個通過 JavaScript 填充的 ASP.NET DropDownList 控制項。但是,我正在使用事件驗證。因此,如果我不使用此處的“RegisterForEventValidation”呼叫作為下拉菜單,我會遇到以下錯誤。我怎麼知道在第二個參數中設置什麼值(我有“值”)?我正在通過 JavaScript 填充我的下拉列表,所以我不知道後面的程式碼中有什麼值。我猜在 AJAX 部分渲染回發期間呼叫了 Render,對嗎?或者不是,所以無論我是否進行整頁回發,都會呼叫它。我想我不僅想听聽我的問題的答案,還想听聽您是否可以與我分享您對以下錯誤的經驗。我喜歡輸入,就像 Johnny #5 一樣。
==================
後面的程式碼:
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.
在閱讀了有關 Script Manager 和反複試驗的廣泛研究之後,我發現了以下內容。
您可以禁用事件驗證,但這並不總是最好的選擇,因為最好同時進行 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