Alex Alex - 1 month ago 4
ASP.NET (C#) Question

How to prevent validation for an event handler of a user control?

I have a user control with a delete button. When a button is clicked, an event fires which deletes a record from the database. Now, the control is placed in

. The whole body markup of
(including the user control with its button itself) resides in
<form runat="server">
as required by ASP.NET. Everything works so far.

However, the problem is when I put some validation controls inside
(meaning inside
<form runat="server">
because otherwise the page will report server errors). When validation controls are added, the delete button in the user control stops working. Clicking on this button no longer triggers the event as before.

Now, I disabled event validation in
directive. I am also including
UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
in the code behind file. However, none of this helps.

How do I fix this problem and make the button clickable?


I know for sure that the validation controls are causing the problem, because I only need to add
to each of them, and the button becomes clickable. But I don't want to rewrite validation on the client side manually!


It turns out the PostBack cannot occur if the form is not IsValid regardless of what element causes it. As long as that element (button) is inside the form with runat="server"that is invalid, the posback will not happen.

A very simple workaround is to just make the Button in my user control bypass validation: CausesValidation="False" (thanks to this question).

Another solution and maybe a more efficient one, is to use ValidationGroup. This way, all TextBoxes together with the Submit Button will belong to one group, and those controls that do not belong to that group will NOT be validated. In fact, they might have their own ValidationGroup; this will avoid interference between different controls within one Web Form.