Tuesday, May 11, 2010
I've been doing some ASP.NET development work in my spare time and came across a customer requirement where the Calendar control used for a reporting system needed to be able to:

  1. change the day that the week started on based on a stored text value
  2. select all days from the current week from today's date back to and including the starting day of the week (whatever that was configured to)

The use of different enumerations for Day surprised me and also the looping logic required took some thinking about when the comparitive integer values of Days could not be used as a means of comparison.

Screenshot and code sample below ran for today's date.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebCalendar
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
_cal.SelectionMode = CalendarSelectionMode.DayWeek;

// setup today's date values
DateTime dtCurrent = DateTime.Now;
DayOfWeek dowCurrent = dtCurrent.DayOfWeek;

// setup day to start week
string strDayToStartWeek = "Thursday"; // test text value - TODO: from database
DayOfWeek dowDayToStartWeek = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), strDayToStartWeek);
FirstDayOfWeek configLeadingDOW = (FirstDayOfWeek)Enum.Parse(typeof(FirstDayOfWeek), strDayToStartWeek);

// set calendar to start week on stored preference
_cal.FirstDayOfWeek = configLeadingDOW;

// select the dates
SelectDatesForWeeklyReport(dtCurrent, dowDayToStartWeek);
}

/// <summary>
/// Select all the days from today back to the day which is nominated as the beginning of the week
/// </summary>
/// <param name="dtCurrent">Today's date</param>
/// <param name="dowDayToStartWeek">Day which is nominated as the beginning of the week</param>
private void SelectDatesForWeeklyReport(DateTime dtCurrent, DayOfWeek dowDayToStartWeek)
{
DateTime dtSelectedDate = dtCurrent;

bool exitLoop = true;

while (exitLoop)
{
if (dtSelectedDate.DayOfWeek == dowDayToStartWeek)
{
// select the start of the week
_cal.SelectedDates.Add(dtSelectedDate);
exitLoop = false;
}
else
{
_cal.SelectedDates.Add(dtSelectedDate);
}
dtSelectedDate = dtSelectedDate.AddDays(-1);
}
}
}
}

posted on Tuesday, May 11, 2010 9:57:49 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback
 Thursday, January 31, 2008
I'm in the process of converting some web applications from v1.1 to v2.0 which is proving interesting.

Initially I used a blank ASP.NET Web Site and added in the web applications using the IIS option and C# library projects.
I got loads of errors and warnings when compiling. I have needed to qualify the use of my Menu class with its namespace as the Menu class is now part of the framework and some new methods for registering client script blocks. A forum post and some time later I decided to start again using the Web Application Project model (ironic aside: under Step 6 there is a link to a MSDN site regarding breaking changes in v2.0 - it just happens to be a broken link) which is a download for VS 2005.

Overall, I have found this better so far as there are less compilation errors. However, one of the few I had I only got 3 results from google and only 1 usable and correct solution.

The whole solution now compiles but there are some hiccups in the execution which I am still sorting out.

posted on Thursday, January 31, 2008 1:52:52 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] Trackback