how should RegisterForEventValidation be used correctly

前端 未结 2 505
半阙折子戏
半阙折子戏 2020-12-16 05:05

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 th

2条回答
  •  萌比男神i
    2020-12-16 05:42

    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.

提交回复
热议问题