Wednesday, July 13, 2011

Disabling validation for Asp.Net controls

When using validation controls in your ASP.NET pages you might want to disable validation in certain situations. The most common example is when you want to disable validation for a Cancel button. You can instruct the ASP.NET server control to disable just the client-side validation, or both the client-side and the server-side validation.


Disabling Client-Side Validation

If you want to perform only server-side validation and to avoid validation on the client, you can specify for certain ASP.NET Server controls not to not run client-side script validation. To disable client-side validation, set the validation control's EnableClientScript property to false.

<asp:Button id="CancelButton" runat="server" Text="Cancel" EnableClientScript="False" />


Disabling both Client-Side and Server-Side Validation

You can specify that individual controls on a Web Forms page cause a postback without triggering a validation check.

If you want to bypass validation for a specific ASP.NET Server control, you’ll have to set the control's CausesValidation property to false. Consider the ASP.NET code example below, showing how to disable validation for a Cancel button:

<asp:Button id="CancelButton" runat="server" Text="Cancel" CausesValidation="False" />

There is another way to disable a validation control, and you can accomplish it by setting the Enabled ASP.NET validation control property to false. Note that if you set Enabled to false, the ASP.NET validation control will not be rendered to the ASP.NET page at all:

<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ControlToValidate="YourControlToValidate" ErrorMessage="Your error message here" Enabled="False" />

No comments:

Post a Comment

If you’re using Visual Studio to build your .NET MAUI app on a Mac, locating the IPA (iOS App Package) file can be a bit tricky

  If you’re using   Visual Studio   to build your   .NET MAUI   app on a   Mac , locating the   IPA (iOS App Package)   file can be a bit tr...