Read, With the Name of Your Lord Who Created

ASP.NET Validation Controls: How to Disable It

Posted by triaslama on December 2, 2008

Validating an input is a common need, sometimes we want a field must be filled, a field must be in a specific range, or an input must follow a special pattern. ASP.NET has a bunch of validation controls that make our job a little bit easy. Despite all of the ease of use offered by ASP.NET validation controls, sometimes -just maybe- we need to disable it.

One important thing we must if we use ASP.NET validation controls is: what actions will trigger validators? One of the answer is button click! (The other maybe Postback action). With that in mind, we can take a conclusion that every click event of asp.net button (<asp:Button />) by default will always trigger validation controls to take the appropriate action.

Let’s say I want to disable RequiredFieldValidator against a TextBox control for one action, and enable it for another action, how can I accomplish this? Consider I have textbox control and two button controls (previous button and next button accordingly). I want if previous button clicked it goes to previous page and doesn’t fire the required field validator. Well, the solution is really simple.

By default each button control will trigger validation control to take an action, unless we override CausesValidation property to false! Yes, that is the solution set CausesValidation property of previous button to false and it will solve the problem.

I drop the code below:

<%@ Page Language="C#" AutoEventWireup="true" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
   <title>Test</title>
</head>
   <body>
      <form id="form1" runat="server">
      <div>
         <asp:TextBox ID="txtInput" runat="server" Width="271px"></asp:TextBox>
         <asp:RequiredFieldValidator ID="txtInputValidator" ControlToValidate="txtInput"
             runat="server" ErrorMessage="This field cannot left blank." Display="Dynamic">
         </asp:RequiredFieldValidator>
         <p />
         <asp:Button ID="btnPrev" PostBackUrl="~/PreviousTest.aspx" CausesValidation="false"
             runat="server" Text="Previous" />&nbsp;
         <asp:Button ID="btnNext" PostBackUrl="~/NextTest.aspx"
             runat="server" Text="Next" Width="81px" />
      </div>
   </form>
</body>
</html>

In above example I assume that PreviousTest.aspx and NextTest.aspx is exist.

Now left the TextBox blank and click ‘Previous’ button. Compare with: left it blank and click ‘Next’ button. To more realize what the role of CausesValidation property change it to true, refresh the page, and do the same thing as before.

14 Responses to “ASP.NET Validation Controls: How to Disable It”

  1. Howie said

    Great! Quick and easy, Thanks!

  2. Sasha said

    the “UseSubmitBehavior” property must also be set to false. By default it is true. So, Although the button will ignore the validator, the
    Required Validator will still fire and display the error message on screen. By setting the “UseSubmitBehavior” to false it will prevent this.

  3. Marinkina said

    Кстати, если закончаться фото Одри, то можешь в фотошопе старые фото накладывать на новый фон, так и разнообразие будет и ты работать продолжишь

  4. Cederash said

    Такой пост и распечатать не жалко, редко такое найдешь в инете, спасибо!

  5. Ferinannnd said

    У автора очень приятный слог

  6. Avertedd said

    Зачет, сенкс автору

  7. robson said

    Thank you
    It was fast and easy.

  8. Santhosh said

    I have the radio button list and a listbox.Each has the validators set. but the value of the first Radio button is hard coded and i dont need to validate it with the list box.Can someone suggest a way.???

  9. Hi I found this site by mistake when i was searching bing for this registry cleaner issue, I must

  10. Rajesh Kumar Soni said

    Very Thanks its quick solution

  11. Direct TV said

    Direct TV…

    […]ASP.NET Validation Controls: How to Disable It « Read, With the Name of Your Lord Who Created[…]…

  12. Informasi Online Perkumpulan Wong Jawa Keluyuran di Sumatera…

    […]ASP.NET Validation Controls: How to Disable It « Read, With the Name of Your Lord Who Created[…]…

  13. phyteney said

    We’re a bunch of volunteers and starting a new scheme in our community.
    Your web site provided us with helpful information to
    work on. You have performed an impressive process and our entire community will probably be grateful to you.

  14. It’s awesome to pay a quick visit this web page and reading the views of all friends regarding this paragraph, while
    I am also keen of getting knowledge.

Leave a reply to Informasi Online Perkumpulan Wong Jawa Keluyuran di Sumatera Cancel reply