Tips and tricks for .NET using ASP and VB code.

From/To calendar control

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="FromToCalendars.ascx.vb"
    Inherits="FromToCalendars" %>
<style type="text/css">
    .DataText /* Style settings for data displayed in user input controls */
    {
        background-color: #FFFFFF;
        font-size: 12pt;
        color: black;
        font-family: 'Courier New' , Monospace;
    }
    .ControlText /* Style settings for the text inside controls */
    {
        font-size: 12pt;
        color: black;
        font-family: Arial, Sans-Serif;
    }
</style>
<table>
    <tr>
        <td align="center">
            <asp:DropDownList ID="FromMonthsDropDownList" runat="server" CssClass="DataText"
                AutoPostBack="True">
                <asp:ListItem Value="1">January</asp:ListItem>
                <asp:ListItem Value="2">February</asp:ListItem>
                <asp:ListItem Value="3">March</asp:ListItem>
                <asp:ListItem Value="4">April</asp:ListItem>
                <asp:ListItem Value="5">May</asp:ListItem>
                <asp:ListItem Value="6">June</asp:ListItem>
                <asp:ListItem Value="7">July</asp:ListItem>
                <asp:ListItem Value="8">August</asp:ListItem>
                <asp:ListItem Value="9">September</asp:ListItem>
                <asp:ListItem Value="10">October</asp:ListItem>
                <asp:ListItem Value="11">November</asp:ListItem>
                <asp:ListItem Value="12">December</asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList ID="FromYearDropDownList" runat="server" CssClass="DataText" AutoPostBack="True">
                <asp:ListItem>2012</asp:ListItem>
            </asp:DropDownList>
            <asp:Calendar ID="FromCalendar" runat="server" FirstDayOfWeek="Monday" BackColor="#FFFFCC"
                BorderColor="#FFCC66" DayNameFormat="Shortest" ForeColor="#663399" ShowGridLines="True"
                CssClass="ControlText">
                <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" />
                <NextPrevStyle ForeColor="#FFFFCC" />
                <OtherMonthDayStyle ForeColor="#CC9966" Font-Size="Smaller" />
                <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
                <SelectorStyle BackColor="#FFCC66" Font-Italic="True" />
                <TitleStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
                <WeekendDayStyle BackColor="#FFFF99" />
            </asp:Calendar>
            <asp:TextBox ID="FromDateTextBox" runat="server" CssClass="DataText" Width="8em"
                Style="text-align: center" AutoPostBack="True"></asp:TextBox>
        </td>
        <td>
            <asp:Label ID="HorizontalOrVerticalLabel" runat="server"> </asp:Label>
        </td>
        <td align="center">
            <asp:DropDownList ID="ToMonthsDropDownList" runat="server" CssClass="DataText" AutoPostBack="True">
                <asp:ListItem Value="1">January</asp:ListItem>
                <asp:ListItem Value="2">February</asp:ListItem>
                <asp:ListItem Value="3">March</asp:ListItem>
                <asp:ListItem Value="4">April</asp:ListItem>
                <asp:ListItem Value="5">May</asp:ListItem>
                <asp:ListItem Value="6">June</asp:ListItem>
                <asp:ListItem Value="7">July</asp:ListItem>
                <asp:ListItem Value="8">August</asp:ListItem>
                <asp:ListItem Value="9">September</asp:ListItem>
                <asp:ListItem Value="10">October</asp:ListItem>
                <asp:ListItem Value="11">November</asp:ListItem>
                <asp:ListItem Value="12">December</asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList ID="ToYearDropDownList" runat="server" CssClass="DataText" AutoPostBack="True">
                <asp:ListItem>2012</asp:ListItem>
            </asp:DropDownList>
            <asp:Calendar ID="ToCalendar" runat="server" FirstDayOfWeek="Monday" BackColor="#FFFFCC"
                BorderColor="#FFCC66" DayNameFormat="Shortest" ForeColor="#663399" ShowGridLines="True"
                CssClass="ControlText">
                <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" />
                <NextPrevStyle ForeColor="#FFFFCC" />
                <OtherMonthDayStyle ForeColor="#CC9966" Font-Size="Smaller" />
                <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
                <SelectorStyle BackColor="#FFCC66" />
                <TitleStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
                <WeekendDayStyle BackColor="#FFFF99" />
            </asp:Calendar>
            <asp:TextBox ID="ToDateTextBox" runat="server" CssClass="DataText" Width="8em" Style="text-align: center"
                AutoPostBack="True"></asp:TextBox>
        </td>
    </tr>
    <tr align="center">
        <td colspan="3">
            <asp:Label ID="TotalDaysLabel" runat="server" CssClass="ControlText">0 Days</asp:Label>
        </td>
    </tr>
</table>
<asp:Label ID="FirstSelectableDateLabel" runat="server" Style="display: none;"></asp:Label>
<asp:Label ID="LastSelectableDateLabel" runat="server" Style="display: none;"></asp:Label>



Partial Class FromToCalendars
Inherits System.Web.UI.UserControl
Partial Class FromToCalendars Inherits System.Web.UI.UserControl Public Event SomethingChanged As System.EventHandler Property FirstDayOfTheWeek As WebControls.FirstDayOfWeek Get Return FromCalendar.FirstDayOfWeek End Get Set(value As System.Web.UI.WebControls.FirstDayOfWeek) FromCalendar.FirstDayOfWeek = value ToCalendar.FirstDayOfWeek = value End Set End Property Property FirstSelectableDate As Date Get If FirstSelectableDateLabel.Text = "" Then FirstSelectableDateLabel.Text = Date.MinValue.ToString End If Dim ReturnDate As New Date If IsDate(FirstSelectableDateLabel.Text) = True Then ReturnDate = Convert.ToDateTime(FirstSelectableDateLabel.Text) End If Return ReturnDate End Get Set(value As Date) FirstSelectableDateLabel.Text = value End Set End Property Property LastSelectableDate As Date Get If LastSelectableDateLabel.Text = "" Then LastSelectableDateLabel.Text = Date.MaxValue.ToString End If Dim ReturnDate As New Date If IsDate(LastSelectableDateLabel.Text) = True Then ReturnDate = Convert.ToDateTime(LastSelectableDateLabel.Text) End If Return ReturnDate End Get Set(value As Date) LastSelectableDateLabel.Text = value End Set End Property Property TotalDays As Integer Get Return Convert.ToInt64(TotalDaysLabel.Text) & " Days" End Get Set(value As Integer) TotalDaysLabel.Text = value End Set End Property Property LayoutDirection As RepeatDirection Get Dim LayoutRepeatDirection As RepeatDirection If HorizontalOrVerticalLabel.Text = "" Then LayoutRepeatDirection = RepeatDirection.Vertical Else LayoutRepeatDirection = RepeatDirection.Horizontal End If Return LayoutRepeatDirection End Get Set(value As RepeatDirection) Select Case value Case RepeatDirection.Horizontal HorizontalOrVerticalLabel.Text = " " Case RepeatDirection.Vertical HorizontalOrVerticalLabel.Text = "" Case Else HorizontalOrVerticalLabel.Text = " " End Select End Set End Property 'Property HorizontalOrVertical As String ' Get ' Dim HorizontalOrVerticalString As String ' If HorizontalOrVerticalLabel.Text = "" Then ' HorizontalOrVerticalString = "Vertical" ' Else ' HorizontalOrVerticalString = "Horizontal" ' End If ' Return HorizontalOrVerticalString ' End Get ' Set(value As String) ' Select Case value ' Case "Horizontal" ' HorizontalOrVerticalLabel.Text = " " ' Case "Vertical" ' HorizontalOrVerticalLabel.Text = "" ' Case Else ' HorizontalOrVerticalLabel.Text = " " ' End Select ' End Set 'End Property Property FromDate As Date Get Return FromCalendar.SelectedDate End Get Set(value As Date) FromCalendar.SelectedDate = value FromDateTextBox.Text = FromCalendar.SelectedDate.ToShortDateString End Set End Property Property ToDate As Date Get Return ToCalendar.SelectedDate End Get Set(value As Date) ToCalendar.SelectedDate = value ToDateTextBox.Text = ToCalendar.SelectedDate.ToShortDateString End Set End Property Protected Sub FromDateTextBox_Load(sender As Object, e As System.EventArgs) Handles FromDateTextBox.Load FromDateTextBox.Width = FromCalendar.Width End Sub Protected Sub ToDateTextBox_Load(sender As Object, e As System.EventArgs) Handles ToDateTextBox.Load ToDateTextBox.Width = ToCalendar.Width End Sub Protected Sub FromDateTextBox_TextChanged(sender As Object, e As System.EventArgs) Handles FromDateTextBox.TextChanged Date.TryParse(FromDateTextBox.Text, FromCalendar.SelectedDate) If FromCalendar.SelectedDate < Me.FirstSelectableDate Then FromCalendar.SelectedDate = Me.FirstSelectableDate End If If FromCalendar.SelectedDate > Me.LastSelectableDate Then FromCalendar.SelectedDate = Me.LastSelectableDate End If FromDateTextBox.Text = FromCalendar.SelectedDate.ToString("M/d/yyyy") FromMonthsDropDownList.SelectedIndex = FromMonthsDropDownList.Items.IndexOf(FromMonthsDropDownList.Items.FindByValue(FromCalendar.SelectedDate.Month)) FromYearDropDownList.SelectedIndex = FromYearDropDownList.Items.IndexOf(FromYearDropDownList.Items.FindByValue(FromCalendar.SelectedDate.Year)) FromCalendar.VisibleDate = FromCalendar.SelectedDate RaiseEvent SomethingChanged(Me, New EventArgs) End Sub Protected Sub ToDateTextBox_TextChanged(sender As Object, e As System.EventArgs) Handles ToDateTextBox.TextChanged Date.TryParse(ToDateTextBox.Text, ToCalendar.SelectedDate) ToCalendar.VisibleDate = ToCalendar.SelectedDate If ToCalendar.SelectedDate < Me.FirstSelectableDate Then ToCalendar.SelectedDate = Me.FirstSelectableDate End If If ToCalendar.SelectedDate > Me.LastSelectableDate Then ToCalendar.SelectedDate = Me.LastSelectableDate End If ToDateTextBox.Text = ToCalendar.SelectedDate.ToString("M/d/yyyy") RaiseEvent SomethingChanged(Me, New EventArgs) End Sub Protected Sub FromCalendar_DayRender(sender As Object, e As System.Web.UI.WebControls.DayRenderEventArgs) Handles FromCalendar.DayRender If FirstSelectableDateLabel.Text > "" Then Dim FirstSelectableDate As New Date Date.TryParse(FirstSelectableDateLabel.Text, FirstSelectableDate) If FirstSelectableDate > e.Day.Date Then e.Day.IsSelectable = False End If End If If LastSelectableDateLabel.Text > "" Then Dim LastSelectableDate As New Date Date.TryParse(LastSelectableDateLabel.Text, LastSelectableDate) If LastSelectableDate < e.Day.Date Then e.Day.IsSelectable = False End If End If End Sub Protected Sub FromCalendar_Init(sender As Object, e As System.EventArgs) Handles FromCalendar.Init If IsPostBack = False Then FromCalendar.SelectedDate = Today FromCalendar.VisibleDate = Today FromCalendar_SelectionChanged(sender, e) End If End Sub Protected Sub ToCalendar_Init(sender As Object, e As System.EventArgs) Handles ToCalendar.Init If IsPostBack = False Then ToCalendar.SelectedDate = Today ToCalendar.VisibleDate = Today ToCalendar_SelectionChanged(sender, e) End If End Sub Protected Sub FromCalendar_SelectionChanged(sender As Object, e As System.EventArgs) Handles FromCalendar.SelectionChanged FromDateTextBox.Text = FromCalendar.SelectedDate.ToString("M/d/yyyy") Dim DaysBetween As TimeSpan = ToCalendar.SelectedDate - FromCalendar.SelectedDate TotalDaysLabel.Text = DaysBetween.TotalDays & " Days" FromMonthsDropDownList.SelectedIndex = FromMonthsDropDownList.Items.IndexOf(FromMonthsDropDownList.Items.FindByValue(FromCalendar.SelectedDate.Month)) FromYearDropDownList.SelectedIndex = FromYearDropDownList.Items.IndexOf(FromYearDropDownList.Items.FindByValue(FromCalendar.SelectedDate.Year)) FromCalendar.VisibleDate = FromCalendar.SelectedDate RaiseEvent SomethingChanged(Me, New EventArgs) End Sub Protected Sub ToCalendar_SelectionChanged(sender As Object, e As System.EventArgs) Handles ToCalendar.SelectionChanged ToDateTextBox.Text = ToCalendar.SelectedDate.ToString("M/d/yyyy") Dim DaysBetween As TimeSpan = ToCalendar.SelectedDate - FromCalendar.SelectedDate TotalDaysLabel.Text = DaysBetween.TotalDays & " Days" ToMonthsDropDownList.SelectedIndex = ToMonthsDropDownList.Items.IndexOf(ToMonthsDropDownList.Items.FindByValue(ToCalendar.SelectedDate.Month)) ToYearDropDownList.SelectedIndex = ToYearDropDownList.Items.IndexOf(ToYearDropDownList.Items.FindByValue(ToCalendar.SelectedDate.Year)) ToCalendar.VisibleDate = ToCalendar.SelectedDate RaiseEvent SomethingChanged(Me, New EventArgs) End Sub Protected Sub FromMonthsDropDownList_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles FromMonthsDropDownList.SelectedIndexChanged FromCalendar.VisibleDate = New Date(FromYearDropDownList.SelectedValue, FromMonthsDropDownList.SelectedValue, 1) End Sub Protected Sub ToMonthsDropDownList_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ToMonthsDropDownList.SelectedIndexChanged ToCalendar.VisibleDate = New Date(ToYearDropDownList.SelectedValue, ToMonthsDropDownList.SelectedValue, 1) End Sub Protected Sub FromYearDropDownList_Load(sender As Object, e As System.EventArgs) Handles FromYearDropDownList.Load If IsPostBack = False Then FromYearDropDownList.Items.Clear() For i As Integer = Today.Year - 100 To Today.Year + 100 FromYearDropDownList.Items.Add(New ListItem(i)) Next FromYearDropDownList.SelectedIndex = FromYearDropDownList.Items.IndexOf(FromYearDropDownList.Items.FindByValue(Today.Year)) End If End Sub Protected Sub ToYearDropDownList_Load(sender As Object, e As System.EventArgs) Handles ToYearDropDownList.Load If IsPostBack = False Then ToYearDropDownList.Items.Clear() For i As Integer = Today.Year - 100 To Today.Year + 100 ToYearDropDownList.Items.Add(New ListItem(i)) Next ToYearDropDownList.SelectedIndex = ToYearDropDownList.Items.IndexOf(ToYearDropDownList.Items.FindByValue(Today.Year)) End If End Sub Protected Sub FromYearDropDownList_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles FromYearDropDownList.SelectedIndexChanged FromCalendar.VisibleDate = New Date(FromYearDropDownList.SelectedValue, FromMonthsDropDownList.SelectedValue, 1) End Sub Protected Sub ToYearDropDownList_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ToYearDropDownList.SelectedIndexChanged ToCalendar.VisibleDate = New Date(ToYearDropDownList.SelectedValue, ToMonthsDropDownList.SelectedValue, 1) End Sub Protected Sub TotalDaysLabel_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles TotalDaysLabel.PreRender Dim DaysBetweenDates As TimeSpan = ToCalendar.SelectedDate - FromCalendar.SelectedDate TotalDaysLabel.Text = DaysBetweenDates.TotalDays & " Days" End Sub Protected Sub ToCalendar_DayRender(sender As Object, e As System.Web.UI.WebControls.DayRenderEventArgs) Handles ToCalendar.DayRender If FirstSelectableDateLabel.Text > "" Then Dim FirstSelectableDate As New Date Date.TryParse(FirstSelectableDateLabel.Text, FirstSelectableDate) If FirstSelectableDate > e.Day.Date Then e.Day.IsSelectable = False End If End If If LastSelectableDateLabel.Text > "" Then Dim LastSelectableDate As New Date Date.TryParse(LastSelectableDateLabel.Text, LastSelectableDate) If LastSelectableDate < e.Day.Date Then e.Day.IsSelectable = False End If End If End Sub End Class

No comments:

Post a Comment

Search This Blog