Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

asp.net - how should RegisterForEventValidation be used correctly

I recently started using ScriptManager. I have an ASP.NET DropDownList control that I'm populating via JavaScript. However, I'm using Event Validation. So I run into the error below if I don't use the "RegisterForEventValidation" call here for my dropdown. How do I know what value(s) to set in the second argument (where I have "value")? I am populating my dropdown via JavaScript, so I won't know what values are there from my code behind. I'm guessing that Render is called during an AJAX partial rendering postback, correct? Or is it not, so this is called regardless of whether I'm doing a full page postback or not. I guess I'm wanting to hear not only the answer to my question, but if you can share your experiences with me about the error below. I love input, just like Johnny #5.

==================

Code behind:

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

    Page.ClientScript.RegisterForEventValidation(DDLTest.UniqueID, "value")
    MyBase.Render(writer)
End Sub

==================

Error:

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After extensive research from reading about Script Manager and trial and error, here is what I found.

You can disable event validation, but that is not always the best option because it's good practice to have both JavaScript validation as well as ASP.NET validation. That is the whole purpose of registering these values in your dropdowns. If you have JavaScript that injects options into your select (a select renders from an ASP.NET DropDownList control) you want to prevent script injection whenever possible.

ANSWER TO MY QUESTION: So to do this, we call this RegisterForEventValidation for EVERY possible value that can ever appear in that dropdown for any possible situation in your application. In my case, I had two dropdowns. One dropdown used to cause a postback and re-populated the second dropdown with values based on the first dropdown. However, now I'm using JavaScript to inject the values into the dropdown with jQuery.

Before re-populating the values, I remove all values with jQuery.

jQuery("#<%=DDLTest.ClientID %>").children("option").each(function() {
  jQuery(this).remove();
});

When my first dropdown changes, I repopulate the second dropdown with the values relevant for the first dropdown value.

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));
});

And select the value I need.

jQuery(this.Elements.DDLTest).val(quickDataEntryObject.TestPricingOptionId);

However, before all this JavaScript stuff happens, I am registering the possible values for the dropdown. You MUST do this in the Render event.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...