Reizar
Reizar

Reputation: 241

Validate ASP.net calendar control against current date

I'm using an ASP.net calendar control and need to validate that the chosen date is after the current date. So if the user tries to choose a date on the calendar that is before the current date, it will give an error.

How can I go about doing this?

Upvotes: 0

Views: 4207

Answers (2)

Raghuveer
Raghuveer

Reputation: 2638

Calender control doesn't have a predefined validator. You have to use a CustomValidator for this purpose

Add a CustomValidator for your Calender Control and check the date OnServerValidate event of CustomValidator method

public void Custom_validaor_Validate(object sender, ServerValidateEventArgs e)
{
    if( calnderDate > DateTime.Now )
    {
        //Code here
    }
}

On your Submit button click check the page is validated or not by saying

public void myButton_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        // the button click event executes even if the page isn't
        // valid, so you have to wrap your save event
        // in this kind of if block to avoid saving bad data to
        // to the database.
    }
}

Upvotes: 1

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

How about

if(calendercontroldate < DateTime.Now.Date)
{
    //Do something
}

Upvotes: 1

Related Questions