Scott on Writing

Musings on technical writing...

Giving .Text a Calendar View

Many blog engines provide a calendar view when visiting the blog via the Web.  Typically the calendar shows the current month, with days that have one or more entries rendered as hyperlinks.  Clicking on the link for a given day displays the posts for that day.  Unfortunately .Text doesn't provide such a built-in calendar view, so I decided to create my own.  (As to why .Text doesn't include a calendar view, Scott Watermasysk indicated that there were issues with the ASP.NET Calendar control and CSS.  Not being a CSS expert, I'm not sure exactly what issues there are, but in my own experiment I'm happy with the rendered display of my calendar, and all stylesheets seem to be playing nicely together...)

In any event, take a minute to check out the Scott on Writing homepage - up in the upper-right hand corner you can see a calendar view.  Pretty cool, eh?  I'd like to share how I accomplished this.

DISCLAIMER: To add the Calendar control I decided to take a path of least resistance, and actually had the entire thing written in under ten minutes.  I used a user control - a compiled control might be a better choice.  There may be more efficient techniques to use than I employed.  If you have suggestions on improvements, feel free to post them; if you want to use the code, feel free, but realize it was created hastily with a “let's get something working before my fiancee gets home from work”-attitude.

TECHNICAL NOTE: I am using .Text 0.94 - I have no idea if this code will work “as-is“ with different versions. I imagine it would, although if the database schema has changed, that would require a slight update of the SQL query used to get the blog entries for a given month/year....

I started by creating a User Control and adding a Calendar.  My aim was to create an event handler for the Calendar's DayRender event, and then see if the particular day being rendered had any posts and, if so, display a hyperlink to the correct /archive/ URL.  (To view the posts on MM/DD/YYYY, you can simply visit /archive/MMDDYYYY.aspx - ah, the power of HTTP handlers.)

Of course, I didn't want to have to hit the database once for each day in the month, so I loaded all of the blog entries from the month into a DataTable (ordered by the date of entry, in ascending order). The SQL query to run (which was moved into a stored procedure) was:

SELECT DateAdded FROM blog_Content (nolock)
WHERE PostType=1 
      AND BlogID = @BlogID
      AND DATEPART(yyyy, DateAdded) = @Year 
      AND DATEPART(m, DateAdded) = @Month
ORDER BY DateAdded

Here, @BlogID is the ID of the blog whose dates of posts you want to show (if you are hosting multiple blogs on a single .Text install, each blog has its own BlogID). @Year and @Month are the year and month of blog entries to get. (The PostType=1 gets only blog entries - not comments, tracebacks, etc.)

In my User Control I have a DataTable (named dates) scoped at the class level that is populated with the results of the above SQL query. The code that performs this follows:

private void LoadMonthData()
{
   SqlConnection myConnection = new SqlConnection(connection string);

   SqlCommand myCommand = new SqlCommand("blog_GetBlogEntriesForMonth", myConnection);
   myCommand.CommandType = CommandType.StoredProcedure

   // selDate is the currently selected date...
   myCommand.Parameters.Add(new SqlParameter("@Year", selDate.Year));
   myCommand.Parameters.Add(new SqlParameter("@Month", selDate.Month));

   SqlParameter blogID = new SqlParameter();
   blogID.Value = 0;
   blogID.ParameterName = "@BlogID";
   myCommand.Parameters.Add(blogID);

   // Fill the dates DataTable...
   SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
   myAdapter.Fill(dates);

   myConnection.Close();

   // Keep track of how many records are in the DataTable...
   dateCount = dates.Rows.Count;
}

In addition to having the dates DataTable scoped at class level, I also have two additional ints: one keeps track of how many total entries there are for the month (dateCount), the other stored the index of the current record being examined in dates (this variable is named currentDate).

In the event handler for the Calendar control's DayRender event, I check to see if the current date being rendered equals the date in the current row in dates being examined. If it does, I change the TableCell's Text property to a hyperlink and update currentDate accordingly - otherwise I do nothing. The code for this event handler follows:

private void dayRender(object sender, DayRenderEventArgs e)
{
   if (currentDate < dateCount &&
         e.Day.Date == Convert.ToDateTime(dates.Rows[currentDate]["DateAdded"]).Date)
   {
      e.Cell.Text = "<A href="\" +="" ?? archive sowBlog>
               String.Format("{0:d2}{1:d2}{2:d4}",
                     e.Day.Date.Month, e.Day.Date.Day, e.Day.Date.Year) +
                     ".aspx\"><U>" + e.Day.Date.Day + "</U></A>";
      // There might be multiple entries per day, so advance currentDate until
      // we're on a different day...
      do
      {
         currentDate++;
      } while ((currentDate < dateCount &&
            e.Day.Date == Convert.ToDateTime(dates.Rows[currentDate]["DateAdded"]).Date));
   }
}

There are some other slight details involved, but the above description spells out the bulk of the work that needed to be done. In order to insert the calendar view I edited the PageTemplate.ascx User Control for my blog's appropriate skin. I also had to do a bit of tweaking with the #leftmenu, #main, and #rightmenu CSS items to get the calendar to fit without forcing the user to have to scroll off to the right.

If you are interested, you download the complete source code for the Calendar User Control here.

posted on Thursday, February 12, 2004 9:13 PM

Feedback

# Ajouter un calendrier 2/13/2004 1:01 AM Julien Cheyssial's Blog (FR)

# re: Giving .Text a Calendar View 2/13/2004 3:49 AM Nick Rigby

Works great with .Text 0.96 :)

# Calendar Control 2/13/2004 5:07 AM ScottWater

How to create a calendar control for .Text

# re: Giving .Text a Calendar View 2/13/2004 8:50 AM Scott Mitchell

Scott Watermasysk has some suggested changes for those using .Text 0.95+ available at his blog: http://scottwater.com/blog/archive/2004/02/13/CalendarControl.aspx

# .Text Calendar control 2/13/2004 2:37 PM ThoughtChain

# Calendar for .Text 2/13/2004 6:25 PM Tech Guru

# An Improved Calendar for .Text 2/14/2004 1:48 AM overflow

# An Improved Calendar for .Text 2/14/2004 1:53 AM overflow

Great control! I created an updated version.

# re: Giving .Text a Calendar View 2/17/2004 12:35 PM Salman

Hey great idea, I remember the earlier versions of .TEXT had this control but I believe for performance reasons ScottW took it out. Nice to see it back.

# Authoring Tools 2/23/2004 1:57 PM Gustin's Ramblin

# Authoring Tools 2/23/2004 1:57 PM Gustin's Ramblin

# Stupidity 3/11/2004 1:27 AM Thoemmi.NET

Remember: if you

# re: Giving .Text a Calendar View 4/3/2004 6:46 PM Pin2

It is extreamly good

# Enhancing Your .Text Blog - Allowing Readers to Rate Blog Entries 4/15/2004 4:15 PM Scott on Writing

# Calendar view in Dottext 0.95 4/26/2004 7:36 AM Maurizio Tammacco Blog

# Text.96 Version Information 4/28/2004 5:56 AM Falling Maple Leaf

Text.96 Version Information

# Text.96 Version Information 4/28/2004 5:57 AM Falling Maple Leaf

Text.96 Version Information

# How to Add a Calendar to .Text 5/29/2004 8:00 PM RoudyBob.NET

# How to Add a Calendar to .Text 5/29/2004 8:11 PM RoudyBob.NET

# New skin for .Text (dottext) - Luxinterior 6/30/2004 4:44 AM Rum, Sodomy and the Lash

Luxinterior - a new skin for .Text (dottext). This article explains how to customise the skin and provides links to download and install the skin.

# New Skin - Clearer and More Readabe! 7/3/2004 7:20 PM CCube Blog

CCube Blog has a new, clearer and more readabe skin!

# New Skin - Clearer and More Readabe! 7/3/2004 7:31 PM CCube Blog

CCube Blog has a new, clearer and more readabe skin!

# New skins for .Text - LuxInterior 7/5/2004 12:19 AM Dennis' blog

# DotText Ratings To Be Added 7/7/2004 6:16 PM Host Content

# .Text Resources 7/11/2004 7:53 AM moeen.com/Blog

# .Text Resources 7/11/2004 7:58 AM moeen.com/Blog

# re: Giving .Text a Calendar View 7/27/2004 6:07 PM theo w

exactly what I was looking for - THANKS

# Added a Calendar to dotText 8/28/2004 1:58 PM Swift Alpha

# Added a Calendar to dotText 8/28/2004 2:01 PM Swift Alpha

# Calendar Control for .Text 9/3/2004 7:53 PM Dean's Blog

# .Text Addons 9/9/2004 9:20 AM DanBartels.Text

# .Text Resourcen 11/10/2004 7:42 AM { shinja.net } blog

# Calendar Control 11/15/2004 7:26 AM Midco System

# Blog Calendar View 12/13/2004 8:52 PM Chris Gastin's Blog

# re: Giving .Text a Calendar View 12/30/2004 6:07 PM RiderDesign

I am using your code on my own blog app (not .Text) to give it a calendar view. However the dayrender sub is not firing. Am i missing something? l aslo converted the code to vbnet as my c# skills are less than perfect.

# re: Giving .Text a Calendar View 12/31/2004 7:24 AM Scott Mitchell

RiderDesign, you need to associate the event handler (the sub) with the Calendar's event. In VB.NET it would look something like:

Sub EventHandlerName(sender as Object, e as XXX) Handles CalendarID.DayRender

Where XXX is the name of the EventHandler class passed into this event handler.

# re: Giving .Text a Calendar View 12/31/2004 8:08 PM RiderDesign

It hit me later on that i hadnt added the handles bit.

# Adding a Calendar Control in .Text 0.95 (from: Dave Burke) 1/10/2005 11:47 AM TOURNEY LOGIC LINK BLOG

# Adding a Calendar Control in .Text 0.95 1/12/2005 6:15 AM Dave Burke's Blog

# Calendar view in Dottext 0.95 1/14/2005 5:04 AM Maurizio Tammacco's Blog

# Enhancement features on .Text 1/29/2005 12:07 AM Kent J. Chen's Weblog

.Text is such a good fundament for the people who is interested in hosting their own blog on .Net and SQL server based platform. At least, I am one of them, and also really enjoy digging into the author's source as well. Someone mentioned some other good blog fundament that contains the good features that .Text doesn't have naturally, such as search, comment block, etc...

# 义乌鲜花店,15花吧,义乌网上花店,义乌网上礼品,义乌网上买花 义乌网上送花,义乌网上鲜花,义乌网上鲜花速递,义乌鲜花 礼品 义乌花店.义乌买花.义乌送花.义乌花网.义乌鲜花速递.义乌鲜花预定.网上订花.鲜花.义乌.花店.鲜花速递义乌china flower,义乌flower,义乌flower delivery,义乌订购鲜花,义乌订花,义乌国际送花,义乌网上订花 http://www.15hua.com 3/13/2005 9:38 PM 义乌鲜花店,15花吧,义乌网上花店,义乌网上礼品,义乌网上买花 义乌网上送花,义乌网上鲜花,义乌网上

?????,15??,??????,??????,?????? ??????,??????,????????,???? ?? ????.????.????.????.??????.??????.????.??.??.??.??????china flower,??flower,??flower delivery,??????,????,??????,?????? http://www.15hua.com

# re: Giving .Text a Calendar View 4/27/2005 11:12 AM ssohl@hntb.com

Do you know how to always make the current date the first date in the calendar control. I cant find this answer anywhere. For example, April 27th would be the first day in the first row......All other days after would be the rest of April and into May.......

# Aanpassen van .Text 8/23/2005 4:37 PM Whatever Blog

# Stupidity 7/24/2006 5:30 AM Thomas Freudenberg's Blog

Remember: if youpatch your blogging engine, make sure everything works properly before publishing it!

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55618.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3020100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1193.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3020100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.00160
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403020

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.67322
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403020

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles