Stachu
Stachu

Reputation: 5857

Why isn't my CustomValidator in ASP.NET 4.0 working?

The controls in the ASP.NET

<asp:TextBox ID="txtEnd" runat="server" placeholder="12:59"></asp:TextBox>
<asp:RadioButtonList ID="rblTime2" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
    <asp:ListItem>AM</asp:ListItem>
    <asp:ListItem>PM</asp:ListItem>
</asp:RadioButtonList>

CustomValidator

<asp:CustomValidator ID="ValidateStartTime" ControlToValidate="txtEnd" OnServerValidate="ValidateStartTimeFun" runat="server" ErrorMessage="*required"></asp:CustomValidator>

CodeBehind

protected void ValidateStartTimeFun(object source, ServerValidateEventArgs args)
{
    try
    {   if (txtStart.Text != "" && rblTime.SelectedValue != null )
        { args.IsValid = true; }}
    catch (Exception ex)
        { args.IsValid = false; }
}

It doesn't even give me a *required if I change the entire CodeBehind to this;

protected void ValidateStartTimeFun(object source, ServerValidateEventArgs args)
{
    args.isValid = false;
}

Upvotes: 2

Views: 2713

Answers (5)

Rashedul.Rubel
Rashedul.Rubel

Reputation: 3584

why don't you use different validator for different controls? and also to work custom validator in server just discard the ControlToValidate property from the tag. and also check the condition you have used in code behind.

Upvotes: 0

gfyans
gfyans

Reputation: 1256

If you're validating empty input, custom validators don't fire if you set the ControlToValidate property AND don't set the ValidateEmptyText property to true, otherwise the framework expects you to use a RequiredFieldValidator.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.validateemptytext.aspx

Upvotes: 2

Kevin Hendricks
Kevin Hendricks

Reputation: 785

It's been a long time since I used it without an updatepanel as this is also an answer I found by Sarawut Positwinyu:

Custom Validator, when placing in formview will not show its error message after
server-side validation (though it has been validated and result is invalid) the 
mean to fix this in to wrap it by a Update Panel. 

I also remember always putting in the validationgroup="name of the group" for all controls which are validatedand putting validateemptytext to true for the custom validator.

answer reference

Upvotes: 0

Emanuele Greco
Emanuele Greco

Reputation: 12731

I think that the ValidationGroup element is missing.

You should the same value for ValidationGroup it to the validator and to the button, too (or checking it programmatically using Validate method of the Page) that has to check the value.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460370

If you set the ControlToValidate property the CustomValidator won't fire if the user doesn't enter/select anything. But if you omit it it will fire always when the form is submitted.

So remove it.

<asp:CustomValidator ID="ValidateEndTime" 
    OnServerValidate="ValidateEndTimeFun" 
    runat="server" ErrorMessage="*required">
</asp:CustomValidator>

Since you're validating multiple controls i would do it that way, otherwise you could also set the ValidateEmptyText property to true as gfyans has mentioned.

Upvotes: 0

Related Questions