当前位置: 首页 > 面试题库 >

如何正确使用RegisterForEventValidation

温智明
2023-03-14
问题内容

我最近开始使用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.

问题答案:

经过阅读有关脚本管理器和反复试验的大量研究之后,发现了以下内容。

您可以禁用事件验证,但这并不总是最好的选择,因为同时进行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事情发生之前,我正在为下拉列表注册可能的值。 您必须在“渲染”事件中执行此操作。

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


 类似资料:
  • 问题内容: 我只想检索UserAccount类中的某些列,所以我有以下代码: 我得到了空值作为回报。但是,如果我注释掉setProjections,我将获得具有所有属性的用户。在这种情况下,如何正确使用setProjection? 问题答案: 它返回一个Object数组,因此代码应为:

  • 问题内容: 我不知道我在哪里错了:/。当我运行这段代码时,我得到的只是一个空白元素。我似乎无法让insertRule方法执行任何操作(甚至不会产生错误)。我想念什么吗? 问题答案: 这有点令人困惑,但是您的代码确实可以工作,只是您看不到返回的XML树中插入的规则。 为了验证您的代码是否有效,您可以执行两个测试: 运行上面的代码片段,您可以看到CSS规则确实适用。并且属性也在控制台中更改。 当浏览器

  • 问题内容: 如何使用从类路径中查找递归资源? 例如 在“目录”中查找所有资源:想象一下 不幸的是,这只会检索到恰好该“目录”。 所有资源都已命名(递归) 但这返回一个空。 还有一个额外的问题:与有什么不同? 问题答案: 没有办法递归搜索类路径。您需要知道资源的完整路径名才能以这种方式检索它。该资源可能位于文件系统中的目录中,也可能位于jar文件中,因此它不像执行“类路径”的目录列表那样简单。您将需

  • 我正在尝试正确地使用ByteBuffer和BigEndian字节顺序格式。。 我有几个字段,我试图把它存储在Cassandra数据库之前放在一个单一的ByteBuffer中。 我将要写入Cassandra的字节数组由三个字节数组组成,如下所述- 现在,我将写,和一起到一个字节数组和由此产生的字节数组我将写入Cassandra,然后我将有我的C程序来检索它字节数组数据从Cassandra,然后反序列

  • 问题内容: 我在用 : Node.js Express 4.0 Passport.js Google OAuth 2身份验证 对于每个用户,我将其Google个人资料中的一些信息(电子邮件等…)存储在MySQL数据库中(我对此技术没有选择),访问和刷新令牌以及用户在提供信息时提供的其他信息他在我的应用上注册。 我已经看到了password.js的不同用法,特别是关于该信息在会话中的存储方式。 在p

  • 我有一个分辨率为2560x1920的图像。我正在对它应用精明的操作符。但是我得到的结果不适合我。我希望所有的边缘都突出来。然而,当我将此算法应用于较低分辨率的图像时,一切都正常工作。我怎样才能解决问题?