Responsive Ads Here

Monday, May 28, 2012

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


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

No comments:

Post a Comment