Using ASP.NET validators on multiple controls - asp.net

can we use the same validator of a control for two different buttons,
or do we need to write add a validator for each control for two different buttons.

Each ASP.NET validator control can be bound to exactly one ControlToValidate, except for the CompareValidator. The CompareValidator also has a ControlToCompare property. You need to add a validator for each control you wish to validate.
When you find that your markup gets bloated with validators, you can also create a handy extension methods on Control and let it automaticaly add a new validator to the page. You can invoke this method in your code behind in OnPreInit or OnInit.

Related

How to except a control from validating when use ASP.NET validation controls?

I have some Textboxs in my .aspx page which has some validation controls (for example requiredValidator control). I want to execute these validation controls when I click only on a special control but not others.
What should I do?
Use CausesValidation=False on the controls which should not trigger validation.
You can also use validation-groups to sepcify what should be validated.

Clientside validation of UserControl with several DropDownLists

[SOLVED] Use WebControl (in this case Panel) to render a DOM-Element with the ClientID as id-attribute, that can be validated.
Didn't find any question/answer to this issue.
I got a UserControl with several DropDownLists (one CheckBox in all of those lists must be checked to get a true validation).
Serverside validation works fine. Clientside validation not.
The OnClickEvent on any of the CheckBoxes doesn't trigger the Javascript code. Also the Sendbutton-Click doesn't trigger the validation for this UserControl.
Is there a way to tell the UserControl to trigger the validation javascript, if any of the Checkboxes in the UserControl gets clicked ?
(btw.: If u use a CustomValidator on CheckBoxLists, it automatically calls the javascript on click on its CheckBoxes, so I think the only Problem is, that i use a Custom UserControl that gets validated by a CustomValidator, so the CustomValidator doesn't get it, that the UserControl has Elements with OnClick-Events)
Solution: Use WebControl (in this case Panel) to render a DOM-Element with the ClientID as id-attribute, that can be validated.

How to validate my own capcha control?

I try to create my own capcha (ctrlCapcha :UserControl) and I would like to use validator, which will display capcha error on ValidationSummary control.
At first I tried to create validator by inherit by BaseValidator class, but I can not pin it to my aspx file (<%# Register %> doesn't work)
At second I tried to use CustomValidator, but when I fix ID of my capcha control asp.net give me error "Control 'ctrlCapcha' referenced by the ControlToValidate property of 'cusValCapcha' cannot be validated." (I service event "OnServerValidate")
Could you tell me which way is better? Thank for you all suggestions :)
What are the fields that are inside the UserControl? I assume you want to validate one of these fields. You can add the Customvalidator to the UserControl itself and implement your ServerValidate inside the codebehind of the user control. You don’t have to set the “ControlToValidate” property. In the ServerValidate, you just add whether validation logic you want.

using same ascx control twice while using Register (ASP.NET)

I'm using Register at the top of the page to register an ascx control, now the thing is I want to use this control twice on the page.
Now, the problem is when I hit buttons of one instance of the control, it fires the validation of both of the controls, and it obviously breaks, because it should be validating only one control - itself! The reason I am sure about this is because if I keep only one instance of the control on the page, then it flows nicely.
What I already tried that did NOT work:
1)Putting the two instances in different ASP Panels.
2)Registering the control twice at top of the page, so each registration has only one instance on the page.
I would not like to modify the validation of the control itself, but it's a huge project and it is being used at other places, and I do not want to disrupt other things. FYI It's using "Page.IsValid" to validate.
Set the ValidationGroup property of the validators and the ValidationGroup property of the buttons dynamically in the Page_Load method of the user control. You can use the user control's ID property as part of the ValidationGroup to differentiate between the two controls.
e.g.,
myRequiredValidator.ValidationGroup = "valGroup_" + this.ID;
myButton.ValidationGroup = "valGroup_" + this.ID;

Creating custom ASP.NET validator

I have created a custom control and a custom validator (extending BaseValidator). On custom control I have set ValidationProperty("Values"). The problem is that validation doesn't work when postback is sent unless I execute Page.Validate(). And when I call Page.Validate() all validators are executed which is not what I would expect on postback.
How do I create custom validator which would be executed when control value changes and validates just that control?
That is not how validators work. Unless you are using a ValidationGroup setting, all the validators on your page will automatically fire. You do NOT have to explicitly call Page.Validate(). You DO need to wrap your code in a check like this, however:
if(Page.IsValid)
{
//do something here
}
Unlike client-side validators, the server-side validation does NOT prevent the page from posting back and processing events as normal.
To create a control which only validates when the control value changes would require a bit of hackery, since the change event fires after the validators have been executed.
Have you tried using validation groups?

Resources