views:

400

answers:

1

Hello all,

I am using C#.net.

I have two textboxes (textbox1 / textbox2). If both are empty it needs to display a error message.

I tried a CustomValidator control and had it validating on textbox2. Within my code behind I checked whether both were empty if so it returned false (args = false). However when I built the application, it didn’t even access the button event. Can I not use this?

Here is what I have tried:

  <asp:CustomValidator ID="customValidator" runat="server" 
    ErrorMessage="You must provide either a phone number or email address." 
    ControlToValidate="textbox2" 
    OnServerValidate="PhoneEmailCustomValidator_ServerValidate" />

protected void PhoneEmailCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (string.IsNullOrEmpty(texbox1.Text) && string.IsNullOrEmpty(textbox2.Text))
            {
                Debug.Write("Within if statement");

                args.IsValid = false;
            }
        }

Thanks in advance for any help.

Clare

+5  A: 

Set the CustomValidator's ValidateEmptyText property to true. Otherwise validation won't be triggered if the validated control is empty.

devio