Field validation of a single button only
Asked Answered
F

4

14

I have the following validator on a textbox inside a modal dialog box.

<asp:RequiredFieldValidator runat = "server" 
                            ErrorMessage = "Role name can not be empty."
                            ControlToValidate = "tbxRoleName" />

It works like it should, except that the validation triggers on every other buttons OnClick handler on the aspx-page too. Since the dialog is invisible it looks like buttons just dont work on the page. My workaround is to add CausesValidation = "false" on all buttons on the page. But it is a very bad solution and I think there should be a smarter way.

Fayefayette answered 13/6, 2011 at 15:3 Comment(0)
F
25

Assign ValidationGroup to each validator and also to the button that should trigger validation (but not the the other button). Something like:

<asp:RequiredFieldValidator ValidationGroup='valGroup1' ... />

<asp:Button ValidationGroup='valGroup1' Text='I trigger validation' ... />
Fimbriate answered 6/2, 2014 at 10:45 Comment(0)
G
9

How about setting a ValidationGroup?

http://msdn.microsoft.com/en-us/library/ms227424.aspx

Gentilesse answered 13/6, 2011 at 15:5 Comment(1)
For those looking at this answer, the technique is to set the ValidationGroup property on the validators and the Button, to associate the Button with a group of validators.Dimitris
M
6

Also you can use 'causesvalidation' to the button. If it is false Button will not response to Validation in aspx page.

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

Metameric answered 23/11, 2017 at 4:16 Comment(0)
C
1

The Button has a property CausesValidation that can disable the validation for that button. More info here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.causesvalidation.aspx

Colporteur answered 13/6, 2011 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.