gpt4 book ai didi

asp.net - 应该如何正确使用RegisterForEventValidation

转载 作者:行者123 更新时间:2023-12-02 15:56:41 25 4
gpt4 key购买 nike

我最近开始使用 ScriptManager。我有一个通过 JavaScript 填充的 ASP.NET DropDownList 控件。但是,我正在使用事件验证。因此,如果我不使用此处的“RegisterForEventValidation”调用作为我的下拉列表,我会遇到以下错误。我如何知道在第二个参数(我有“值”)中设置什么值?我通过 JavaScript 填充下拉列表,因此我不知道后面的代码中有哪些值。我猜测 Render 在 AJAX 部分渲染回发期间被调用,对吗?或者不是,所以无论我是否进行整页回发,都会调用此函数。我想我不仅想听到我的问题的答案,而且还想听听您是否可以与我分享您对以下错误的经验。我喜欢输入,就像 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.

最佳答案

经过对脚本管理器的阅读和反复试验进行了广泛的研究,以下是我的发现。

您可以禁用事件验证,但这并不总是最佳选择,因为同时进行 JavaScript 验证和 ASP.NET 验证是一种很好的做法。这就是在下拉列表中注册这些值的全部目的。如果您有 JavaScript 将选项注入(inject)到您的选择中(选择从 ASP.NET DropDownList 控件呈现),您希望尽可能防止脚本注入(inject)。

回答我的问题:为此,我们将针对应用程序中任何可能情况出现在该下拉列表中的每个可能值称为 RegisterForEventValidation。 在我的例子中,我有两个下拉菜单。一个下拉列表用于引起回发,并使用基于第一个下拉列表的值重新填充第二个下拉列表。但是,现在我使用 JavaScript 通过 jQuery 将值注入(inject)到下拉列表中。

在重新填充值之前,我使用 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 内容发生之前,我正在注册下拉列表的可能值。您必须在渲染事件中执行此操作。

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

关于asp.net - 应该如何正确使用RegisterForEventValidation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5942244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com