Vikas
Vikas

Reputation: 24332

How to customize calendar control?

I am displaying calendar using calendar control.

Each date is displayed as Link button.

But i want only last 30 days(dates) from current date, to be enabled for client click. on those days i will write some code on SelectionChanged event. But goal is other dates must be disabled for click.

They should not have link button.

Upvotes: 0

Views: 492

Answers (2)

Konstantin Tarkus
Konstantin Tarkus

Reputation: 38428

Try to write custom day rendering function and attach it to   DayRender   event of your Calendar control. It should be pretty simple.

Something like:

<asp:Calendar ID="C1" runat="server" OnDayRender="Calendar1_DayRender" />

and

private void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    if (e.Day.Date < BeginningOfDateRange ||
        e.Day.Date > EndOfDateRange)
    {
        e.Cell.BackColor = System.Drawing.Color.LightGray
        e.Day.IsSelectable = False
    }
}

Upvotes: 1

M4N
M4N

Reputation: 96606

You can handle the DayRender event of the calendar control to customize the appearance of each day displayed. This event handler is called for each day displayed by the calendar control:

<asp:Calendar ID="Calendar1" runat="server" OnDayRender="Calendar1_DayRender">
</asp:Calendar>

And in the code-behind:

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    e.Day.IsSelectable = IsDateValid(e.Day.Date);
}

Where IsDateValid() is a method you implement to check whether a date should be enabled or not.

Upvotes: 1

Related Questions