Sunday, 1 June 2008

DateTime Validation in DynamicData

Step 1 add a CustomValidator to the CustomUserControl a copy of the DateTime_Edit.ascx (or just modify the existing DateTime_Edit.ascx user control)

<asp:CustomValidator ID="CustomValidator1" runat="server" 
    ErrorMessage="Invalid DateTime"
    ControlToValidate="TextBox1" 
    OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>

Step 2 Add a server side event OnServerValidate="CustomValidator1_ServerValidate" and an error message.

protected void Page_Load(object sender, EventArgs e)
{
    TextBox1.ToolTip = Column.Description;

    //SetUpValidator(RequiredFieldValidator1);
    //SetUpValidator(RegularExpressionValidator1);
    //SetUpValidator(DynamicValidator1);
}

Step 3 In the Page_Load event comment out the three SetUpValidator calls.

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
    DateTime tempDateTime;
    String textDateTime = TextBox1.Text;
    if (DateTime.TryParse(textDateTime, out tempDateTime))
    {
        args.IsValid = true;
    }
    else
    {
        args.IsValid = false;
    }
}

Step 4 add you server side event handler for the OnServerValidate event and you've done it.

2 comments:

Anonymous said...

thanks dude worked for me

Anonymous said...

whats that column.description