Z5 Concepts

ASP.NET C#.NET - Using CausesValidation

by on 13. September 2010 23:03

    In ASP.NET, when using the RequiredFieldValidator control on a textbox, you must always enter a value in the textbox before postback occurs. In the event you have a form which has a textbox that cannot be null or must have a value, you want to use the ASP.NET RequiredFieldValidator control.

    On most forms, you typically have a submit button. You also should have either a cancel button, or a reset button of some sort to either refresh the page, or clear the form fields. If this is the case, and you have a requiredfieldvalidator control tied to one of your textbox controls, then whenever the textbox is empty, and a user clicks cancel, your requiredfieldvalidator control will kick in and state you must have a value in the textbox.

    There is a way around this and that is using the "CausesValidation" property. CausesValidation can be added to many different ASP.NET controls. In this instance, you will want to add it to your cancel button control. See example below for more details.





  <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>

   <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"




   <asp:Button ID="btnSubmit" runat="server" Text="Submit" />

   <asp:Button ID="btnCancel" runat="server" Text="Cancel" CausesValidation="false" />




     If you do not understand how this works or need help, please comment, and I'd be glad to help. Or feel free to go to our contact page and send us an e-mail.




ASP.NET | C#.NET | Troubleshooting | Visual Studio 2005 | Visual Studio 2008 | Websites

Comments are closed

Theme by Z5 Concepts