Mac*_*ver 7 asp.net validation ajax events
我最近开始使用ScriptManager.我有一个ASP.NET DropDownList控件,我通过JavaScript填充.但是,我正在使用事件验证.如果我没有在这里使用"RegisterForEventValidation"调用我的下拉列表,那么我会遇到下面的错误.我如何知道在第二个参数中设置的值(我有"值")?我通过JavaScript填充我的下拉列表,所以我不知道我的代码后面有什么值.我猜测在AJAX部分渲染回发期间调用Render,对吗?或者不是,所以无论我是否正在进行整页回发,都会调用它.我想我不仅希望听到我的问题的答案,而且如果你能与我分享关于下面的错误的经验.我喜欢输入,就像约翰尼#5.
==================
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Page.ClientScript.RegisterForEventValidation(DDLTest.UniqueID, "value")
MyBase.Render(writer)
End Sub
Run Code Online (Sandbox Code Playgroud)
==================
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.
Run Code Online (Sandbox Code Playgroud)
Mac*_*ver 10
经过阅读有关脚本管理器和反复试验的广泛研究,这是我发现的.
您可以禁用事件验证,但这并不总是最佳选择,因为同时进行JavaScript验证和ASP.NET验证是一种很好的做法.这是在您的下拉列表中注册这些值的全部目的.如果您有JavaScript将选项注入您的选择(从ASP.NET DropDownList控件中选择渲染),您希望尽可能防止脚本注入.
回答我的问题:为此,我们将此RegisterForEventValidation称为可能出现在应用程序中任何可能情况的下拉列表中的每个可能值. 在我的情况下,我有两个下拉菜单.一个下拉列表用于导致回发并使用基于第一个下拉列表的值重新填充第二个下拉列表.但是,现在我使用JavaScript将值注入到jQuery的下拉列表中.
在重新填充值之前,我使用jQuery删除所有值.
jQuery("#<%=DDLTest.ClientID %>").children("option").each(function() {
jQuery(this).remove();
});
Run Code Online (Sandbox Code Playgroud)
当我的第一个下拉列表发生更改时,我会使用与第一个下拉列表值相关的值重新填充第二个下拉列表.
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));
});
Run Code Online (Sandbox Code Playgroud)
并选择我需要的值.
jQuery(this.Elements.DDLTest).val(quickDataEntryObject.TestPricingOptionId);
Run Code Online (Sandbox Code Playgroud)
但是,在所有这些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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23173 次 |
| 最近记录: |