Disabling Validation for ASP.NET Server 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” />

Advertisements

Diganta is an experienced Technical Program Manager with a passion for technology. He has architected and developed software for more than a decade for a broad range of industries. Diganta is a founder of two online IT businesses. He is a certified AWS Solutions Architect, AWS Certified Developer, Professional Scrum Master (PSM I), Professional Scrum Developer (PSD I) and ITIL Certified. He has presented at Microsoft Tech.Ed, Microsoft AppFest and Ark Group Intranet conference in the past. He attends AWS Seattle Official Events and Seattle AWS Architects-Engineers. He likes to help, mentor, and manage software development teams to improve and produce great software.

Tagged with:
Posted in C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s