Disable Future Dates on asp:calendar control - asp.net

How do I disable future dates on an asp:calendar control?
For example, today is October 22 2013, I want to disable dates from October 23 onward,
and re-enable them when it is the current date.

You will need to use the DayRender method.
C# Code Behind
public void Calendar1_DayRender(object o, DayRenderEventArgs e)
{
e.Cell.BackColor = System.Drawing.Color.Empty;
e.Cell.ForeColor = System.Drawing.Color.DarkRed;
//if the days are not part of the current month
if (e.Day.IsOtherMonth)
{
e.Cell.Text = "";
}
}
Then in the calendar, you need to place
ASP
<asp:Calendar ID="Calendar1" runat="server" Height="145px" Width="77px" OnDayRender="Calendar1_DayRender"></asp:Calendar>
You add the OnDayRender="Calendar1_DayRender" to the calendar control.
Hope this helps!
EDIT: I misread that. But if you want to just to "enable" the current day and no other days, then this will work..
Code Behind
public void Calendar1_DayRender(object o, DayRenderEventArgs e)
{
e.Cell.BackColor = System.Drawing.Color.Empty;
e.Cell.ForeColor = System.Drawing.Color.DarkRed;
if (e.Day.IsToday)
{
e.Cell.BackColor = System.Drawing.Color.Blue;
}
else
{
e.Cell.Text = "";
}
}

You can use this trick to disable future dates in Calendar Control.
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date > DateTime.Today)
{
e.Day.IsSelectable = false;
}
}

Related

How to disable Current Date's Week in Calendar Control

I am trying to disable Current date's week in the calendar control. Can anyone tell me how to do this. For example... Today is August 21, 2012. I want that when calendar is loaded is should display the whole week from August 20 to August 25 as Disabled.
Hello you can use with ondayrender
<asp:Calendar ID="id" runat="server" ondayrender="Disabled_DayRender"></asp:Calendar>
protected void Disabled_DayRender(object sender, DayRenderEventArgs e)
{
if (condition) //specify your condition on yours days
{
e.Day.IsSelectable = false;
e.Cell.ForeColor = System.Drawing.Color.Gray;
}
}
You can calculate week number of year and then compare it with current date's week number.
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (GetNumber(e.Day.Date) == GetNumber(DateTime.Now.Date))
{
e.Day.IsSelectable = false;
e.Cell.ForeColor = System.Drawing.Color.Gray;
}
}
public static int GetNumber(DateTime dtDate)
{
System.Globalization.CultureInfo ciGetNumber = System.Globalization.CultureInfo.CurrentCulture;
int returnNumber = ciGetNumber.Calendar.GetWeekOfYear(dtDate, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday);
return returnNumber;
}
This would do the trick. Thanks to Princy.

Customize Calendar control

I want to customize the calendar control in such a way that calendar starts from current date.
E.g. Today is 29th February then it should show the first date 9th February in the calendar.
Is it possible to do with asp.net calendar control?
By default the <asp:Calendar /> control displays the current month.
This can be overriden using the VisibleDate property if you like.
Could you please clarify what you mean that it should start from todays date? What should happen to the rest of the days of the month?
If you want to grey out or disable the other days in the month, hook into the OnDayRender event which will run when each day is rendered and it gives you access to the date and the cell so you can do something like.
protected void Calendar1_OnDayRender(object sender, DayRenderEventArgs e) {
if(e.Day.Date < DateTime.Now) {
e.Cell.CssClass="disabled";
}
}
The other property you need to know of is the SelectedDate property. Setting this is the equivalent of clicking on that particular date.
You ca set the SelectedDate propery. Assuming your calendar name is MyCal, you can set the first month date in this way:
myCal.SelectedDate = New Date(DateTime.Now.Year, DateTime.Now.Month, 1)
ADDENDUM
Try with this code:
protected void Page_Load(object sender, EventArgs e)
{
MyCal.SelectedDate = DateTime.Now.Date;
}
protected void MyCal_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date < DateTime.Now.Date)
{
e.Cell.Visible = false;
}
}
protected void MyCal_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
{
if (e.NewDate.Month == DateTime.Now.Date.Month - 1)
{
MyCal.SelectedDate = e.PreviousDate;
MyCal.VisibleDate = e.PreviousDate;
}
}
In this way you:
Set the current date
Hide the previous day
Disable previous month browsing

C# code for selecting only from todays date from a calendar

Hi iam using a calendar control in my application,i need to select only the date from todays date and future date. am using this calendar control in the content place holder,can any one help me out with this problem.
Thanks in advance
Try this
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date < DateTime.Now.Date)
{
e.Day.IsSelectable = false;
}
}

Asp.net calendar select current week in page load event

In the Page_Load event I would like to have the current week selected on the calendar instead of just the current day. How can this be done?
<asp:Calendar
ID="Calendar1"
runat="server"
ondayrender="Calendar1_DayRender"
SelectionMode="DayWeek"
onselectionchanged="Calendar1_SelectionChanged">
</asp:Calendar>
protected void Page_Load(object sender, EventArgs e)
{
Calendar1.SelectedDate = System.DateTime.Today;
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
e.Day.IsSelectable = false;
}
Give this a try:
Calendar1.SelectionMode = CalendarSelectionMode.DayWeek;
ArrayList selectedDates = new ArrayList();
DateTime today = DateTime.Now;
DateTime firstDay = today.AddDays(-(double)(today.DayOfWeek));
for (int loop = 0; loop < 7; loop++)
Calendar1.SelectedDates.Add(firstDay.AddDays(loop));

disable sunday in asp.net calender

How to disable Sunday in asp.net calendar control? I am using C#.
You can create a method for the dayrender event in the control. Something like this:
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
if (e.Day.Date.DayOfWeek != DayOfWeek.Sunday) return;
e.Cell.ApplyStyle(new Style { BackColor = System.Drawing.Color.Gray });
e.Day.IsSelectable = false;
}

Resources