ASP.NET Code
  Home arrow ASP.NET Code arrow ASP.Net: Calendar Web Control by James Che...
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET CODE

ASP.Net: Calendar Web Control by James Chen
By: aspfree
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 32
    2002-07-07

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    ASP.Net: Calendar Web Control

    Introduction

    Calendar web control is provided by Microsoft.Net framework, it can display a calendar in a web page, user can navigate to any day in any year, user can also select dates, and it can also used to display some information, like appointment or schedule, in the calendar.

    This article gives you an example application of programming on Calendar control. We have a XML file which has holiday data in year 2002, we use a DataSet object to read this XML file and mark them on the Calendar, when you select a date, the date and a description, if it is a holiday, will be displayed in a Label control.

    In this article I will provide a step-by-step guide to show you how to:

    1. Read XML file into a DataSet object
    2. Display the DataSet data in a DataGrid
    3. Program Calendar control to display information
    4. Program Calendar control when user select a date

    Requirement

    You need the following things to do this work

    1. Windows 2000 Pro or Server with service pack 2 as your operation system.
    2. Visual Studio.Net is installed on your system

    Create a XML file and read it into a DataSet object

    1. Start your Visual Studio.Net, from the File menu, point to New, and then click Project to display the New Project dialog box.
    2. Select Visual C# Projects in the Project Types pane, and then select ASP.Net Web Application. Name the project Calendar, then click OK. Visual Studio will add this project to Solution Explorer and display a new web page in the designer.
    3. From the File menu, point to New, and then click File. The New File dialog box appears.
    4. Select XML File and click Open.
    5. Paste the following code into the editor below the XML declaration, (Right-click the editor and select Paste as HTML )
      <holidays>
      
      <holiday>
      <Date>2002/1/1</Date>
      <Description>New Years' Day</Description>
      </holiday>
      <holiday>
      <Date>2002/1/15</Date>
      <Description>Martin Luther King</Description>
      </holiday>
      <holiday>
      <Date>2002/2/19</Date>
      <Description>President's Day</Description>
      </holiday>
      <holiday>
      <Date>2002/3/28</Date>
      <Description>Memorial Day</Description>
      </holiday>
      <holiday>
      <Date>2002/7/4</Date>
      <Description>Independence Day</Description>
      </holiday>
      <holiday>
      <Date>2002/9/3</Date>
      <Description>Labor Day</Description>
      </holiday>
      <holiday>
      <Date>2002/10/8</Date>
      <Description>Columbus Day</Description>
      </holiday>
      <holiday>
      <Date>2002/11/12</Date>
      <Description>Veterans Day</Description>
      </holiday>
      <holiday>
      <Date>2002/11/22</Date>
      <Description>Thanksgiving Day</Description>
      </holiday>
      <holiday>
      <Date>2002/12/25</Date>
      <Description>Christmas Day</Description>
      </holiday>
      </holidays>
    6. From the File menu, point to Save XMLFile1 As. Name the file holiday.xml and click Save.
    7. In Solution Explorer window, select WebForm1.aspx, right click and select View Code.
    8. Declare a DataSet Object, under public class WebForm1
      protected DataSet dsHoliday = new DataSet("holiday");
      
      In private void Page_Load method, write this:
      dsHoliday.ReadXml("C:\Inetpub\wwwroot\Calendar\holiday.xml");
      
    9. Now, you have read the XML file into your DataSet object.

     

    Display the XML file in a DataGrid control

    In order to prove the XML file is loaded into DateSet successfully, we use a DataGrid control to display the XML data in the page. You do not need this step if you don't want to confirm the data. However, I suggest you do it and then hide the DataGrid control by setting its Visible property to false .
    1. From the Web Forms tab of the Toolbox, drag a DataGrid control onto the design view of WebForm1.aspx page.
    2. Add the following three lines into the private void Page_Load method
      DataGrid1.DataSource = dsHoliday;
      
      DataGrid1.DataMember = "holiday";
      DataGrid1.DataBind();

    3. In Solution Explorer, right-click the page and choose Build and Browse. Confirm that a list of holiday is displayed in the grid.


    Programming on the Calendar control

    Now the XML data is already in our DataSet object, we need to do is mark them in the Calendar control by programming on the DayRender event of Calendar object. When the Calendar control is generating the calendar data, it raises an event you can handle. You create a event handler to hook this event, the control calls your event handler for each day as it is preparing the day for display, and you can then programmatically examine which date is being rendered and customize it appropriately.

    1. From the Web Forms tab of the Toolbox, drag a Calendar control onto the design view of WebForm1.aspx page.
    2. If you like you can change the appearance by clicking Auto Format... like under the Properties window, you can also change individual style properties to change the appearance.
    3. In Properties window, click Event button, and select DayRender event, double click it. This will create an event handler in your code view of WebForm1.aspx page.
      private void Calendar1_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
    4. Copy the following code into this Calendar1_DayRender method.
      DataGrid1.DataSource = dsHoliday;
      
      Style holidayStyle = new Style();
      holidayStyle.ForeColor = System.Drawing.Color.Red;

      foreach (DataRow myDataRow in dsHoliday.Tables["holiday"].Rows)
      {
      string s = myDataRow["Date"].ToString();
      DateTime holiday_date = DateTime.Parse(s);
      if ( (e.Day.Date == holiday_date) &&
      (!e.Day.IsOtherMonth) )
      {
      e.Cell.ApplyStyle(holidayStyle);
      }
      }
      At first, set the DataSource of DataGrid1 to dsHoliday DataSet. Then create a Style to mark the calendar red for the holidays.
      When Calendar control generate the calendar data, this DayRender event is fired for every day in the calendar, the date is in the parameter DayRenderEventArgs e, the above code compares the date in parameter e and every record in dsHoliday DataSet, if the date in parameter e equals to a date in dsHoliday record, then apply the holidayStyle to that date cell in calendar.
    5. Build the project and run, you will see the Calendar control displayed in your browser, then navigate it to the months with a holiday, you will see the holiday date is displayed in red.

     

    Programming When User Select a Date from Calendar

    When user click on a date on a Calendar control, the Calendar control fires a SelectionChanged Event, developer can add a event handler and some code to program it. In this sample, when user click on a date, the date string, like Wednesday, July 17, 2002, will be displayed in a Label control, if that date is a holiday, the description of the holiday is also displayed.

    1. In Properties window, click Event button, and select SelectionChanged event, double click it. This will create an event handler in your code view of WebForm1.aspx page.
      private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
    2. In the design view of WebForm1.aspx, drag a Label control from the Toolbox.
    3. Copy the following code into this Calendar1_SelectionChanged method.
      
      
      string holiday_descr = "";
      if (e != null){
      System.Web.UI.WebControls.Calendar cl =
      (System.Web.UI.WebControls.Calendar)sender;
      foreach (DataRow myDataRow in dsHoliday.Tables["holiday"].Rows)
      {
      DateTime holiday_date = DateTime.Parse(myDataRow["Date"].ToString());
      if ( cl.SelectedDate.Date == holiday_date ){
      holiday_descr = ", " + myDataRow["Description"].ToString();
      }
      }
      Label1.Text = cl.SelectedDate.Date.ToLongDateString() + holiday_descr;
      }
      When the Calendar control fires this event, the parameter object sender is the Calendar control itself, the code above will cast the sender to Calendar object, then you can use SelectedDate property of Calendar control. The above code will go over all the records to find whether the selected date is a holiday, if it is, then get the description of the holiday.
    4. Build the project and run it, when you click on a date, the date string will be displayed in the Label control.




    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More ASP.NET Code Articles
    More By aspfree

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Achieving True Agility -- How process can change the behavior of your tools

    Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools.
    FREE! Go There Now!


    NEW! Download DB2 Express-C 9.5

    Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages.
    FREE! Go There Now!


    NEW! Download IBM Rational Developer for System z

    Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects.
    FREE! Go There Now!


    NEW! Improve your build process with IBM Rational Build Forge, Part 2: Automate builds for a real-world Tomcat project

    Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available.
    FREE! Go There Now!


    NEW! Rational Talks to You:Per Kroll on Rational Method Composer Plug-in customization

    Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered!
    FREE! Go There Now!


    NEW! Spot defects early with Continuous Integration

    Continuous Integration (or CI) is a process that consists of continuously compiling, testing, inspecting, and deploying source code. In many Continuous Integration environments, this means running a new build anytime code within a source code management repository changes. The benefit of CI is simple: assembling software often greatly increases the likelihood that you will spot defects early, when they still are relatively manageable. In this tutorial, a companion to his series In pursuit of code quality, Andrew Glover introduces the fundamental aspects of Continuous Integration and steps you through how to set up a CI process using best-of-breed open source technologies.
    FREE! Go There Now!


    NEW! Travels with Mark: A Hitchhiker&apos;s Guide to the UniVerse, Part 5: Increase dynamic array processing performance in IBM UniVerse

    Investigate the effects of field-level caching in dynamic array access, in part 5 of the UniVerse performance series.
    FREE! Go There Now!


    NEW! Trial download: IBM Informix Dynamic Server Express Edition V11.0

    Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data.
    FREE! Go There Now!


    NEW! Trial download: IBM Lotus Forms V3.0

    Get a free trial download of IBM Lotus Forms V3.0 (formerly Workplace Forms), which provides a zero-footprint eForms solution to help you automate and move forms-based business processes off the desktop and onto the Web. With Lotus Forms, you can extend applications beyond the firewall by creating a single electronic form document ready for use in both thick and Web 2.0 thin client format.
    FREE! Go There Now!


    NEW! Webcast: Eclipse: Empowering the universal platform

    The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    ASP.NET CODE ARTICLES

    - How to Use the ListBox Control in ASP.NET 2.0
    - How to Load XML Documents in ASP.NET 2.0
    - DataGrid Code
    - ASP.NET Guestbook
    - User Controls and Client Side Scripting
    - ASP.NET Programming with Microsoft's AS...
    - ASP.NET Basics (part 3): Hard Choices
    - ASP.NET Basics (part 2): Not My Type
    - ASP.NET Basics (part 1): Nothing But .Net
    - Directory Tree Browser
    - How to get the confirmation of Yes/No from a...
    - Complete example using custom errors and wri...
    - Paging Certain # records per page .NET style
    - General Methods of formatting and Subtractin...
    - .NET LinkButton web control




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway