A Date Picker Snippet

This is a snippet I created to insert the controls needed to create a date picker. I use the AjaxControl Toolkit’s Calendar Extender control to create a date picker. This also inserts a couple validation controls to make it required and to make sure it is a proper date.

Below is the source for my snippet.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Insert Date Picker</Title>
      <Shortcut>ssdtp</Shortcut>
      <Description>Code snippet to insert an asp:textbox that uses the AjaxControlToolkit's calendar extender, an asp:image control, and several validation controls.</Description>
      <Author>SheldonS</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>txtDate</ID>
          <ToolTip>ID of date text box control.</ToolTip>
          <Default>txtDate</Default>
        </Literal>
        <Literal>
          <ID>imgDate</ID>
          <ToolTip>ID of the image control.</ToolTip>
          <Default>imgDate</Default>
        </Literal>
        <Literal>
          <ID>valrDate</ID>
          <ToolTip>ID of the required field validation control.</ToolTip>
          <Default>valrDate</Default>
        </Literal>
        <Literal>
          <ID>caleDate</ID>
          <ToolTip>ID of the calendar extender control.</ToolTip>
          <Default>caleDate</Default>
        </Literal>
        <Literal>
          <ID>vceRDate</ID>
          <ToolTip>ID of the required field validator callout extender.</ToolTip>
          <Default>vceRDate</Default>
        </Literal>
        <Literal>
          <ID>valxDate</ID>
          <ToolTip>ID of the regular expression field validation control.</ToolTip>
          <Default>valxDate</Default>
        </Literal>
        <Literal>
          <ID>vceXDate</ID>
          <ToolTip>ID of the regular expression field validator callout extender.</ToolTip>
          <Default>vceXDate</Default>
        </Literal>
      </Declarations>
      <Code Language="html">
        <![CDATA[
        <asp:TextBox ID="$txtDate$" runat="server" Width="150" />
        <asp:Image ID="$imgDate$" runat="server" ImageUrl="~/Images/calendar.jpg" CssClass="clickable" />
        <cc1:CalendarExtender ID="$caleDate$" runat="server" TargetControlID="$txtDate$" PopupButtonID="$imgDate$" />
        <asp:RequiredFieldValidator ID="$valrDate$" runat="server" ControlToValidate="$txtDate$" 
            Text="*" ErrorMessage="You must provide a Date." SetFocusOnError="true" 
            Display="Dynamic">
        </asp:RequiredFieldValidator>
        <cc1:ValidatorCalloutExtender ID="$vceRDate$" runat="server" TargetControlID="$valrDate$" PopupPosition="Right" ViewStateMode="Enabled">
        </cc1:ValidatorCalloutExtender>
        <asp:RegularExpressionValidator ID="$valxDate$" runat="server" ControlToValidate="$txtDate$" 
            Text="*" ErrorMessage="You must enter a proper Date mm/dd/yyyy." SetFocusOnError="true" 
            Display="Dynamic" ValidationExpression="^(((((((0?[13578])|(1[02]))[\.\-/]?((0?[1-9])|([12]\d)|(3[01])))|(((0?[469])|(11))[\.\-/]?((0?[1-9])|([12]\d)|(30)))|((0?2)[\.\-/]?((0?[1-9])|(1\d)|(2[0-8]))))[\.\-/]?(((19)|(20))?([\d][\d]))))|((0?2)[\.\-/]?(29)[\.\-/]?(((19)|(20))?(([02468][048])|([13579][26])))))$">
        </asp:RegularExpressionValidator>
        <cc1:ValidatorCalloutExtender ID="$vceXDate$" runat="server" TargetControlID="$valxDate$" PopupPosition="Right" ViewStateMode="Enabled">
        </cc1:ValidatorCalloutExtender>
        ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

UPDATE: Here is the source for the insertdatepicker.snippet file as generated by Snippet Designer.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>InsertDatePicker</Title>
      <Author>SheldonS</Author>
      <Description>Creates a group of controls to act like a date picker with validator controls.  Uses controls from the AjaxControl Toolkit.</Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>ssdatepicker</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>txtDate</ID>
          <ToolTip>ID of the asp:TextBox control to hold the date.</ToolTip>
          <Default>txtDate</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>imgDate</ID>
          <ToolTip>IDof the asp:Image control of the calendar image.</ToolTip>
          <Default>imgDate</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>caleDate</ID>
          <ToolTip>IDof the AjaxControlToolkit CalendarExtender control.</ToolTip>
          <Default>caleDate</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>valrDate</ID>
          <ToolTip>ID of the asp:RequiredFieldValidator control.</ToolTip>
          <Default>valrDate</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>vceRDate</ID>
          <ToolTip>ID of the AjaxControlToolkit CalloutExtender for the RequiredFieldValidator control.</ToolTip>
          <Default>vceRDate</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>valxDate</ID>
          <ToolTip>IDof the asp:RegularExpressionValidator control.</ToolTip>
          <Default>valxDate</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>vceXDate</ID>
          <ToolTip>ID of the AjaxControlToolkit CalloutExtender for the asp:RegularExpressionValidator control.</ToolTip>
          <Default>vceXDate</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="html"><![CDATA[<asp:TextBox ID="$txtDate$" runat="server" Width="150" />
<asp:Image ID="$imgDate$" runat="server" ImageUrl="~/Images/calendar.jpg" CssClass="clickable" />
<cc1:CalendarExtender ID="$caleDate$" runat="server" TargetControlID="$txtDate$" PopupButtonID="$imgDate$" />
<asp:RequiredFieldValidator ID="$valrDate$" runat="server" ControlToValidate="$txtDate$"
    Text="*" ErrorMessage="You must provide a Date." SetFocusOnError="true" Display="Dynamic">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="$vceRDate$" runat="server" TargetControlID="$valrDate$"
    PopupPosition="Right" ViewStateMode="Enabled">
</cc1:ValidatorCalloutExtender>
<asp:RegularExpressionValidator ID="$valxDate$" runat="server" ControlToValidate="$txtDate$"
    Text="*" ErrorMessage="You must enter a proper Date mm/dd/yyyy." SetFocusOnError="true"
    Display="Dynamic" ValidationExpression="^(((((((0?[13578])|(1[02]))[\.\-/]?((0?[1-9])|([12]\d)|(3[01])))|(((0?[469])|(11))[\.\-/]?((0?[1-9])|([12]\d)|(30)))|((0?2)[\.\-/]?((0?[1-9])|(1\d)|(2[0-8]))))[\.\-/]?(((19)|(20))?([\d][\d]))))|((0?2)[\.\-/]?(29)[\.\-/]?(((19)|(20))?(([02468][048])|([13579][26])))))&">
</asp:RegularExpressionValidator>
<cc1:ValidatorCalloutExtender ID="$vceXDate$" runat="server" TargetControlID="$valxDate$"
    PopupPosition="Right" ViewStateMode="Enabled">
</cc1:ValidatorCalloutExtender>]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
Advertisements

About SheldonS

Web developer for over 15 years mainly with Microsoft technologies from classic ASP to .NET 4. Husband, father, and aspiring amateur photographer.

Posted on June 6, 2011, in Snippet. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: