Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Field validation of a single button only

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.

like image 368
Björn Lindqvist Avatar asked Jun 13 '11 15:06

Björn Lindqvist


People also ask

What are the five types of validation controls explain?

ASP.Net provides various validation controls that validate the user data to ensure that the data entered by the user are satisfied with the condition. ASP.Net provides RequiredFieldValidator, RangeValidator, CompareValidator, RegularExpressionValidator, CustomValidator and ValidationSummary.

Why do we need required field validator?

ASP.NET RequiredFieldValidator Control This validator is used to make an input control required. It will throw an error if user leaves input control empty. It is used to mandate form control required and restrict the user to provide data.


2 Answers

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' ... />
like image 118
Vishal Zala Avatar answered Oct 04 '22 19:10

Vishal Zala


How about setting a ValidationGroup?

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

like image 21
ek_ny Avatar answered Oct 04 '22 17:10

ek_ny