public static class DropDownHelper
{
public static void PopulateFromEnum<T>(this DropDownList drpist)
{
Type enumType = typeof(T);
if (enumType.BaseType != typeof(Enum))
throw new ArgumentException("T must be of type System.Enum");
Array enumValArray = Enum.GetValues(enumType);
foreach (string s in Enum.GetNames(enumType))
drpist.Items.Add(new ListItem(s.Replace("_", " "), Convert.ToInt16(Enum.Parse(enumType, s)).ToString()));
}
}
public static class CheckBoxListHelper
{
public static void PopulateFromEnum<T>(this CheckBoxList cblist)
{
Type enumType = typeof(T);
if (enumType.BaseType != typeof(Enum))
throw new ArgumentException("T must be of type System.Enum");
Array enumValArray = Enum.GetValues(enumType);
foreach (string s in Enum.GetNames(enumType))
cblist.Items.Add(new ListItem(s.Replace("_", " "), Convert.ToInt16(Enum.Parse(enumType, s)).ToString()));
}
}
public static class DateTimeHelper
{
/// <summary>
/// Given a datetime object will return a datetime object for the monday of that week.
/// If argument datetime is a Monday then the return object will be a copy of the input argument.
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static DateTime FindMonday(this DateTime dateTime)
{
DateTime rtnDate = dateTime;
while (rtnDate.DayOfWeek != DayOfWeek.Monday)
rtnDate = rtnDate.AddDays(-1);
return rtnDate;
}
/// <summary>
/// Given datetime object returns a datetime object for the first day for the month of the argument datetime
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static DateTime FindFirstOfMonth(this DateTime dateTime)
{
DateTime rtnVal = dateTime;
while (rtnVal.Day != 1)
rtnVal = rtnVal.AddDays(-1);
return rtnVal;
}
/// <summary>
/// Return datetime object for the first day of the year for the datetime argument
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static DateTime FindFirstOfYear(this DateTime dateTime)
{
DateTime rtnVal = dateTime;
while (rtnVal.DayOfYear != 1)
rtnVal = rtnVal.AddDays(-1);
return rtnVal;
}
}