Wednesday, March 21, 2012

How to disable previous dates in Calendar control in ASP.NET

 

This is a simple code but many people got confused about this inbuilt feature in .NET Calendar control.

We will be using Calendar DayRender control to disable all the previous dates.

First we will add the Calendar control,

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

No we will see what you have to write under DayRender event,

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            if (e.Day.Date < DateTime.Now.Date)
            {
                e.Day.IsSelectable = false;
                e.Cell.ForeColor = System.Drawing.Color.Gray;
            }
        }

 

See the Calendar control after disabling the previous dates,

Calendar

3 comments:

Unknown said...

It was a wonderfull code .It works for me

Anonymous said...

You have been very helpful. Thanks

IT Tutorials said...


It is really a great work and the way in which u r sharing the knowledge is excellent.
Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.
Dot Net Training in chennai | top10 dot net training institutes in chennai