When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML
Information:
Advertise
Feedback
Author an Article
Jobs
















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this Page!

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Saturday, March 2, 2002

Populating Form Inputs Using the Calendar Control, Part 2
By James Avery


  • Read Part 1

  • In Part 1 we looked at how to create a general calendar control and explained the aim of this article: to create a calendar pop-up to facilitate selecting a date for a form field text box. In this part we'll complete the application by examining how to "beautify" our calendar control and add the essential functionality.

    - continued -

    Making the Calendar Control Pretty!
    Since by default there is no selected date on the calendar we are going to capture the OnDayRender event so we can change the background color of the current date to a different color. We could also change the font size, or make the date bold if we wanted to. We can easily accomplish this by adding an event handler to our control like so:

    <asp:Calendar id="id" runat="server"
            OnSelectionChanged="eventHandler"
            OnDayRender="methodName" />

    Here the EventHandler is the name of the evant handler that you must provide to handle the OnDayRender event. Note that for my application I created a function named Caledar1_dayrender to override handle this event.

    There are a few more properties we may wish to set to make the calendar control more useful. One important one is to set the SelectionMode to Day, which limits the user to selecting a single day as opposed to a range of dates. There are a number of UI/stylistic properties you may wish to set as well, such as BorderColor, BackColor, DayNameFormat, TitleStyle, etc. You should consult the documentation for more information on the properties of the calendar Web control.

    At this point, we have created a calendar control, set some properties, and specified an event handler for the OnSelectionChanged and OnDayRender events.

    <%@ Page Language="vb" %>
    <script runat="server">
    Private Sub Calendar1_SelectionChanged(sender As Object, e As EventArgs)
        'TODO: Add code to emit client-side JavaScript to auto-populate
        '      the date text field in the form
    End Sub
        
    Private Sub Calendar1_DayRender(sender As Object, e As DayRenderEventArgs)
       'TODO: Add code to render the selected day
    End Sub
    </script>
    
    <form runat="server">
        <asp:Calendar id="Calendar1" runat="server" 
                         OnSelectionChanged="Calendar1_SelectionChanged" 
                         OnDayRender="Calendar1_dayrender" 
                         ShowTitle="true" DayNameFormat="FirstTwoLetters" 
                         SelectionMode="Day" BackColor="#ffffff" 
                         FirstDayOfWeek="Monday" BorderColor="#000000" 
                         ForeColor="#00000" Height="60" Width="120">
            <TitleStyle backcolor="#000080" forecolor="#ffffff" />
            <NextPrevStyle backcolor="#000080" forecolor="#ffffff" />
            <OtherMonthDayStyle forecolor="#c0c0c0" />
        </asp:Calendar>
        <asp:Literal id="Literal1" runat="server"></asp:Literal>
    </form>
    

    Note that we still need to add code for our Calendar1_SelectionChanged event handler and Calendar1_DayRender method. Let's first look at the Calendar1_DayRender method. We simply want to use this method to highlight the current date when the user loads the calendar. To accomplish this, we simply need to add these three lines of code:

    Private Sub Calendar1_DayRender(sender As Object, e As DayRenderEventArgs)
       If e.Day.Date = DateTime.Now.ToString("d") Then
         e.Cell.BackColor = System.Drawing.Color.LightGray
       End If
    End Sub
    

    Notice that we use the properties Day and Cell which are made available through the DayRenderEventArgs object. The Cell property represents the cell around the date and the day property is the date being rendered.

    Now all that remains is the Calendar1_SelectionChanged event handler, where we need to emit client-side JavaScript code to update the form and close the pop-up. While we could simply use Response.Write's, the output would appear before any other HTML content (including the <html> tag). So, to make our HTML "correct," we'll use a literal control inbetween the <head> ... </head> tags. Creating this literal control is easy enough:

    <head>
       <asp:Literal id="Literal1" runat="server"></asp:Literal>
    </head>

    We will use this literal control to hold our JavaScript after we create it. Now lets look at creating the JavaScript to pass the value back to the form. The first thing we will do is create a string, strJScript, to hold our JavaScript code. Next, we will build up this string so that it's end contents set the form's text box's value to the selected date and the window is closed. This is accomplished with the following code in the Calendar1_SelectionChanged event handler.

    Private Sub Calendar1_SelectionChanged(sender As Object, e As EventArgs)
      Dim strjscript as string = "<script language=""javascript"">"
      strjscript &= "window.opener." & _
            Httpcontext.Current.Request.Querystring("formname") & ".value = '" & _
            Calendar1.SelectedDate & "';window.close();"
      strjscript = strjscript & "</script" & ">" 'Don't Ask, Tool Bug
        
      Literal1.Text = strjscript  'Set the literal control's text to the JScript code
    End Sub
    

    Here we round out the event handler by setting the text of our literal to the dynamically generated client-side JavaScript code. This has the effect of executing this client-side JavaScript code when the page is rendered. So when a user selects a date this event will fire and the JavaScript will be created. As the page is rendered the JavaScript will be run, the value of the selected date transferred to the form's text box, and the calendar page closed. Neat!

    Happy Programming!

  • By James Avery


    Attachments

  • Download the code for calendar.aspx in VB.NET code
  • Download the code for calendar.aspx in C# code


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
    Microsoft Article: 7.0, Microsoft's Lucky Version?
    Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Windows Server 2008
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES