how should RegisterForEventValidation be used correctly how should RegisterForEventValidation be used correctly ajax ajax

how should RegisterForEventValidation be used correctly


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


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.