Tuesday, April 5, 2011

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;
    }
From stackoverflow
  • Hi Joshua

    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));   
    
    Joshua Slocum : perfect thank you

0 comments:

Post a Comment