Click here to Skip to main content
15,887,262 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am using Microsoft Visual Studio 2010 on Windows XP with either IE 8 or Mozilla Firefox.

I have a TextBox that I am trying to use a CustomValidator with. However, I can't seem to get the OnServerValidate event to fire.

Here's my ASP:

ASP
<asp:TextBox runat="server" Width="280" maxlength="80" id="txtEmail" />
<asp:CustomValidator runat="server" ID="cvEmail" ControlToValidate="txtEmail"
     SetFocusOnError="true" ValidateEmptyText="true" OnServerValidate="cvEmail_OnServerValidate" />

Listing 1. Code to set up the Custom Validator.

Here is the code-behind:

C#
protected void cvEmail_OnServerValidate(object sender, ServerValidateEventArgs e)
    {
        // validate the email address in the text box using our Email Validator
        if (_validator != null)
        {
            string errorMessageText = string.Empty;
            if (_validator.ValidateEmailAddress(e.Value,
                ref errorMessageText))
            {
                // display the error message text
                errorMessageLabel.Font.Bold = true;
                errorMessageLabel.Text = errorMessageText;
                e.IsValid = false;
            }
            else
                e.IsValid = true;
        }
    }

Listing 2. Code-behind for the OnServerValidate event.

It should be noted that _validator is a field which points to another class in the project, and the _validator field is allocated in Page_Load.

Thanks for any insight!

Brian


UPDATE:
Resolved by OP himself - posted as one of the solutions.
Posted
Updated 6-Apr-11 8:44am
v2

1 solution

My solution is that of course this was not firing; I had a RequiredFieldValidator elsewhere on the page for another TextBox and the CustomFieldValidator only fires if the other validator is also satisfied; otherwise, there's no point, since the page is already invalid.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900