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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

For event validation to work property, you have to register every possible POST value for the control using RegisterForEventValidation. You can invoke this method multiple times for the same control to register multiple value.

Sometimes, this is not possible – for example, in your case where you are populating drop-down dynamically from java-scripts. Solution(s) would be

  1. Disable event validation for entire page
  2. Use some clever tricks – for example, register some value for event validation that preferably be always present in the drop-down list (e.g. “–Select–“), on submit, put the selected drop-down value into a hidden field and set the drop-down selected value to the value that we used for registration. You may need to add that value to drop-down if not present.
  3. Write your own custom control that does not participate in event validation or does it as per your needs.

I generally go by #1 or #3 based on requirements/budget etc.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x