我最近開始使用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.
@MacGuyver,錯了,您不必在Render()方法中調用RegisterForEventValidation。您可以在調用Render()方法期間或之前調用RegisterForEventValidation。 – Bradley 2013-03-28 14:04:30
@ user2220060感謝您澄清。自從我看了這一年已經過去了一年多了。只是爲了幫助其他讀者,你能列出你可以從中調用的其他方法嗎?我記得ASP.NET在整頁回發期間有一系列事件觸發,但我不記得它們是什麼。 – MacGyver 2013-07-16 21:19:39
@Bradley你確定嗎?我試圖在預渲染期間運行它,並得到錯誤消息:「System.InvalidOperationException:'RegisterForEventValidation只能在Render();''中調用 – 2017-06-08 18:56:11