How to use RegisterForEventValidation correctly

I recently started using ScriptManager. I have an ASP.NET DropDownList control that I populate using JavaScript. However, I use event checking. Therefore, I encounter the error below if I do not use the "RegisterForEventValidation" call here for my drop-down list. How do I know what value to set in the second argument (where I have a "value")? I populate my dropdown with JavaScript, so I won’t know what values ​​are from my code. I assume that Render gets called during partial AJAX rendering, right? Or it’s not, so it’s called regardless of whether I do a full postback of the page or not. I probably want to hear not only the answer to my question, but if you can share your experience with me about the error below. I like the entrance, 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 

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

Mistake:

 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. 
+7
source share
2 answers

After extensive research read about Script Manager and trial and error, this is what I found.

You can turn off event checking, but this is not always the best option, because both JavaScript and ASP.NET validation are good practices. This is the purpose of registering these values ​​in the drop-down lists. If you have JavaScript that introduces options to your choice (select to display from an ASP.NET DropDownList control), you want to prevent Script injection when possible.

ANSWER MY QUESTION: To do this, we call it RegisterForEventValidation for EVERY possible value that may appear in this drop-down list for any possible situation in your application. In my case, I had two dropdowns. One drop-down menu used to create the postback and re-populate the second drop-down list with values ​​based on the first drop-down list. However, now I use JavaScript to enter values ​​into the drop-down list using jQuery.

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

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

When my first drop-down menu changes, I re-populate the second drop-down menu with values ​​corresponding to the first drop-down 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 desired value.

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

However, before all this happens, I register the possible values ​​for the drop-down list. 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 
+10
source

To verify the health of the event, you need to register all possible POST values ​​for the control using RegisterForEventValidation . You can call this method several times for the same control to register multiple values.

Sometimes this is not possible - for example, in your case, when you dynamically drop out of Java scripts. Solution would be

  • Disable event checking for the entire page
  • Use some clever tricks - for example, register some value for checking events, which is always desirable to be present in the drop-down list (for example, "--Select--"), when sending, put the selected drop-down value in a hidden field and set the drop-down selected value to which we used to register. You may need to add this value to the drop-down menu if it is missing.
  • Create your own custom control that does not participate in event verification or does not perform it according to your needs.

I usually go # 1 or # 3 based on requirements / budget, etc.

+2
source

All Articles