This blog has moved!

Check out www.CodeBetter.com/blogs/grant.killian

<September 2008>
SuMoTuWeThFrSa
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011


Navigation

Professional Props...

Extracurricular Props...

Subscriptions

Article Categories



Extending ASP:Calendar with Poor-Man's DataBinding

It all started when the customer had a simple requirement for a calendar in their application; no problem: asp:calendar control gets called in for duty!  Then came the 3 words every developer hates to hear when meeting with a customer: “It's nice, but . . .”.  The “It's nice, but . . .“ statement can launch applications into the world of scope creep and project death march, so I am always cautious when I hear customers begin with the dreaded 3.

This time, however, was harmless: the customer just wanted a title to display along with the day in the calendar control.  It's not as easy as it sounds and I began to wonder if I was <movieReference name=“Zoolander“>taking crazy pills</movieReference>!

A few friends of mine had complained about trouble with “Databinding a web calendar control” and rolled their own variation on the DataList -- yuck!  I also did a search on databinding a .Net Calendar control and discovered this link: www.c-sharpcorner.com/Code/2003/July/ASPNetCalendarControl.asp; while helpful, it didn't get close to my specific needs, but I did adapt the UI for my solution from this article by Sushila D. Patel.  It began to look like creating my own control deriving from the WebControls.Calendar class was the way to go with this! 

So here is the code from the “Control Library“, the key is the call to getLabel that returns a string to include beneath the number in each calendar day.  getLabel accesses a DataTable instance variable that we set in any client for the control:

 public class optCalendar : System.Web.UI.WebControls.Calendar
 {
  //Provide custom implementation of the OnDayRender method
  protected override void OnDayRender ( System.Web.UI.WebControls.TableCell cell ,
   System.Web.UI.WebControls.CalendarDay day )
  {
   string strLabel = getLabel( day.Date.ToShortDateString() );//key invocation
   if( !strLabel.Equals( "" ) )
   {
    Label lbl = new Label();
    lbl.Text = "<BR>" + strLabel;
    cell.Controls.Add( lbl );    
   }
   base.OnDayRender( cell, day ); //let the base class do the hard work!
  }

  private DataTable _dt; //just the setters and getters for the public DataTable property 
  public DataTable DayLabelTable
  {
   get{ return _dt; }
   set{ _dt = value; }
  }

  protected string getLabel( string strDate )
  {
   foreach( DataRow dr in _dt.Rows )
   {
    if( DateTime.Parse( dr[ "EventDate" ].ToString() ).ToShortDateString().Equals( strDate ) )
    {
     return dr[ "EventName" ].ToString();
    }
   }
   return "";
  }
 }

The code from the UI that invokes the control library includes this in the HTML, if this is foreign to you, you may want to check out the Intro to Web Custom Controls on MSDN.

<%@ Register TagPrefix="opt" Namespace="optControlLibrary" Assembly="optControlLibrary" %>

<opt:optCalendar id="optCal" OnDayRender="CalendarRender" runat="server"></opt:optCalendar>

And this in the page_load event, it wires up our DataTable to “bind“ our calendar to:

optCal.DayLabelTable = ds.Tables[ 0 ]; //could be more ingenious here, but for the time being this will suffice

Now, this CalendarRender method assumes a datatable containing a table with “EventDate” and “EventName” are columns in the table ds.Tables[ 0 ]; I will probably refactor this so my control has only 2 strongly typed columns avoid hard coded column names that must match. 

Here is a screen shot of the calendar, with the titles rendering as Labels under the date:

 Demo Calendar

There's no rocket science to this, but it does seem harder than it should be . . . and I'm suspicious that I've missed something obvious in the DataBinding of a calendar control.  In a sense, this is a custom databinding implementation.  Maybe I'll rework this into a more DataBinding conformant model.  This control will eventually be cleaned up and make it into our release-version control library at work, so I may post the improvements once I get around to them.

Happy .Netting!

posted on Monday, April 19, 2004 9:36 AM by grant.killian





Powered by Dot Net Junkies, by Telligent Systems