I have a User Control and it's got a bool IsValidDate property. How can I use CustomValidator to check for this value and return its error message if the value of the property is false?
Asked
Active
Viewed 1,334 times
0
-
may be this is what you're looking for http://stackoverflow.com/questions/939802/date-validation-with-asp-net-validator – coder Apr 16 '12 at 07:50
1 Answers
1
If your user control looks something like this:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyDateUserControl.ascx.cs" Inherits="CustomValidation.MyDateUserControl" %>
My custom user control
<asp:TextBox runat="server" ID="DateTextBox" />
<asp:CustomValidator runat="server" ValidateEmptyText="true" ID="DateCustomValidator" ControlToValidate="DateTextBox" OnServerValidate="DateCustomValidator_ServerValidate" ErrorMessage="The date is not valid" />
<asp:Button ID="SubmitButton" runat="server" Text="Submit" />
Then in your codebehind you can use:
public bool IsValidDate
{
get
{
DateTime temp;
return DateTime.TryParse(DateTextBox.Text, out temp);
}
}
protected void DateCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = IsValidDate;
}
If you don't want your custom validator to be a part of your user control, you have to prefix IsValidDate
with the name of your user control.

Wouter de Kort
- 39,090
- 12
- 84
- 103