How to bypass validation for a button in ASP.NET?

I have an ASP.NET form that takes input from a user. There’s a Submit button on the form and a button called Calc which does a calculation to populate a text field. The problem I’m having is that on the form I have a set of <ASP:REQUIREDFIELDVALIDATOR> validators and when the Calc button is pressed the form gets validated. I don’t want the required fields to be validated when the Calc button is pressed, only the Submit button. Any way around this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Set the CausesValidation property to false.

Method 2

<asp:Button runat="Server" ... CausesValidation="False" />

Button.CausesValidation (If I remember correctly).

Method 3

Try putting CausesValidation="false" as a button attribute.

Some sample code:

http://weblogs.asp.net/scottgu/archive/2005/08/04/421647.aspx

Method 4

ASPX Page:

<asp:Button ID="buttonNew" runat="server" Text="New" CausesValidation="False" />

OR

CodeBehind Page: (.cs)

buttonNew.CausesValidation = false;

Check here to know more about Validated and Validating events for the controls.

Method 5

Set the button.causesValidation to false.

this link

However, if all it is doing is calculating something based on user input then you shouldn’t have it posting back at all. I would recommend using an HTML button and attach some javascript to it to do your work for you and then you won’t have this problem.

Method 6

While designing the button, you can set its property CausesValidation=”false” to avoid validation on button click event. It does not allow to validation the server control and perform its click event only

Method 7

You should use this

UseSubmitBehavior="False"

Method 8

To disable validation in a specific control

Set the control’s CausesValidation property to false.

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

To disable a validation control

Set the validation controls Enabled property to false.

To disable client-side validation

Set the validation controls EnableClientScript property to false.

For More Info


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x