+2  A: 

Looking at the public members exposed by the ASP.NET Calendar control I do not believe that this information is something that you can just get from the calendar control.

You have a few options as "workarounds" to this though, although not nice....but they would work.

  1. You could manually calculate the first week values
  2. You can handle the "day render" event to handle the binding of the individual days, and record min/max values.

Granted neither is elegant, but AFAIK it is the only real option

Edit

After discussion in the comments, another option is a modified version of my second option above. Basically the first time Day Render is called, get the block of data for the next 42 days, then you can simply search the list for the proper day value to display on future calls to DayRender, avoiding a DB hit for each day. Doing this is another "non-elegant" solution, but it works, and reduces a bit of load on the DB, but introduces some overhead on the application side.

It will be important here to define well structured page level properties to hold the items during the binding events, but to ensure that if a month changed, etc that it wasn't loaded incorrectly etc.

Mitchel Sellers
The thing is, I need to know the range that will be rendered before I can get data from my data tier, so using DayRender isn't an option. I'm probably going to do #1, although I hate the idea of trying to reverse engineer the necessary algorithm... seems very error prone. Thanks though!
Seth Petry-Johnson
Is there a reason you can't just make a dB call on dayrender to get the exact data you need?
Mitchel Sellers
@Mitchel: That would result in a separate DB hit for each day. I'd rather determine the date range that will be displayed and then get all of that data, nothing more and nothing less, in a single DB call.
Seth Petry-Johnson
@Mitchel: Unless you're suggesting I use the _first_ DayRender call to get the data for the entire range? I guess I could determine it's the first render call, add 42 days, and then get the data... it's not perfect, but it has potential. Could you add that as an answer?
Seth Petry-Johnson
@seth: Will add that option. One thing though in all of my testing of creating calendars and I've created quite a few, the overhead of multiple DB hits, although a bit of an effect, for the simplicity of implementation, it is overall a possible solution.
Mitchel Sellers
A: 

Mitchel, Worked perfectly, thank you. Started with a public variable bool m_FirstDay = false

in the day_render function

if(m_FirstDay == false) { DateTime firstDate; DateTime lastDate;

firstDate = e.Day.Date;
lastDate = firstDate.AddDays(41);

m_FirstDay = true;

}

I then had the visible date range of the asp.net calendar control. Thanks again.

+1  A: 

I wrote a couple of methods to help with this. Just pass in Calendar.VisibleDate:

public static DateTime GetFirstDateOfMonth(DateTime date)
{
    return new DateTime(date.Year, date.Month, 1);
}

public static DateTime GetFirstDisplayedDate(DateTime date)
{
    date = GetFirstDateOfMonth(date);
    return date.DayOfWeek == DayOfWeek.Sunday ? date.AddDays(-7) : date.AddDays((int)date.DayOfWeek * -1);
}

public static List<DateTime> GetDisplayedDates(DateTime date)
{
    date = GetFirstDisplayedDate(date);

    List<DateTime> dates = new List<DateTime>();
    for (int i = 0; i < 42; i++)
    {
        dates.Add(date.AddDays(i));
    }

    return dates;
}
Ronnie Overby
A: 
Daniel Ives