A Day in the Life of a Developer
TECHNOLOGIES: ASP.NET Calendar Control | DHTML
Build a Date Picker
Learn to use the Calendar control without complex HTML and postback structures.
By Doug Seven
Microsoft ASP.NET MVP
The Parent Window
In a Web form, I display a TextBox that holds a date and a hyperlink that links to the DatePicker Web form. This Web form is shown in Figure 1.
Figure 1. The parent Web form has a TextBox that holds a date and a link to the DatePicker Web form.
<%@ Page Language="vb" AutoEventWireup="false"
content="Microsoft Visual Studio.NET 7.0">
window.open("DatePicker.aspx?src=" + Src,
"height=260, width=250, left=100, top=100, " +
"location=no, menubar=no, resizable=no, " +
"scrollbars=no, titlebar=no, toolbar=no", true);
Figure 2. When you click on the Date Picker link, the pickDate() function is invoked and a new browser window opens.
The DatePicker Web Form
The new browser window that opens when the pickDate function is invoked (the DatePicker.aspx Web form) contains a Calendar control and a LinkButton control (see Figure 3).
Figure 3. This window incorporates both the Calendar control and the LinkButton control, letting the user view calendar months and select a date before closing the window.
I created an event handler for LinkButton that retrieves the SelectedDate property of the Calendar control and passes it back to the TextBox control in the parent Web form. I did this using the window.opener property, which refers to the window that opens the current window. This gives me full access to the DHTML DOM of the parent window.
Private Sub LinkButton1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles LinkButton1.Click
Dim script As New System.Text.StringBuilder()
script.Append("") 'Closing script tag
'Add the script to the page
The code for the date picker is available for download.
As one of the co-founders of DotNetJunkies (http://www.dotnetjunkies.com), a content-based online training resource for .NET developers, Doug Seven has been building applications with the .NET Framework since summer 2000. Seven has co-authored five books related to the .NET Framework, including Programming Data-Driven Web Applications with ASP.NET and ASP.NET: Tips, Tutorials & Code (Sams), and he has worked with C#, Visual Basic .NET, Web applications, mobile device applications, XML Web Services, Windows Form development, and console and service applications. E-mail Doug at mailto:[email protected].