Click here to Skip to main content
15,891,777 members
Articles / Web Development / ASP.NET

Databound Schedule Controls

Rate me:
Please Sign up or sign in to vote.
4.82/5 (176 votes)
24 Mar 2013LGPL321 min read 2.2M   42.2K   632  
These data controls can show scheduled events automatically
<%@ Page Language="vb" %>
<%@ Register TagPrefix="rw" Namespace="rw" Assembly="schedule2" %>
<script runat="server">

    Sub Page_Load(Sender As Object, E As EventArgs)
    
        If Not (IsPostBack) Then
            Dim i As Integer
            For i = 0 To 6
                ddlStartDay.Items.Add(CType(i, DayOfWeek).ToString())
            Next i
            ddlStartDay.SelectedIndex = 1 ' Select Monday
            ' Typically, don't use a fixed date here
            ' use something like Today.AddDays(-14) instead
            Schedule1.StartDate = New Date(2004, 3, 8)
            tbStartDate.Text = Schedule1.StartDate.ToShortDateString()
        End If
    End Sub
       
    Sub btnRepetition_Click(ByVal sender As Object, ByVal e As EventArgs)
        Schedule1.NumberOfRepetitions = CInt(tbNumberOfRepetitions.Text)
        Schedule1.NumberOfDays = CInt(tbNumberOfDays.Text)
        ddlStartDay.Enabled = (Schedule1.NumberOfDays = 7)
        Schedule1.DataBind()
    End Sub
    
    Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Schedule1.Layout = IIf(RadioButtonList1.SelectedValue = "Horizontal", LayoutEnum.Horizontal, LayoutEnum.Vertical)
        tbNumberOfRepetitions.Enabled = (RadioButtonList1.SelectedValue = "Vertical")
        Schedule1.DataBind()
    End Sub
    
    Sub cbIncludeEnd_CheckedChanged(sender As Object, e As EventArgs)
        Schedule1.IncludeEndValue = cbIncludeEnd.Checked
        Schedule1.DataBind()
    End Sub
    
    Sub cbTimeScale_CheckedChanged(sender As Object, e As EventArgs)
        Schedule1.FullTimeScale=cbTimeScale.Checked
        btnApply.Enabled=cbTimeScale.Checked
        If(cbTimeScale.Checked) Then
            btnApply_Click(sender, e)
        Else
            Schedule1.DataBind()
        End If
    End Sub
    
    Sub btnApply_Click(sender As Object, e As EventArgs)
        Schedule1.TimeScaleInterval = CInt(tbInterval.Text)
        Schedule1.StartOfTimeScale = TimeSpan.Parse(tbStart.Text)
        Schedule1.EndOfTimeScale = TimeSpan.Parse(tbEnd.Text)
        Schedule1.DataBind()
    End Sub
    
    Sub btnDate_Click(sender As Object, e As EventArgs)
        Schedule1.StartDate = DateTime.Parse(tbStartDate.Text)
        Schedule1.DataBind()
    End Sub

    Protected Sub cbValueMarks_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Schedule1.ShowValueMarks = cbValueMarks.Checked
        Schedule1.DataBind()
    End Sub
    
    Protected Sub ddlStartDay_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Schedule1.StartDay = CType(ddlStartDay.SelectedIndex, DayOfWeek)
        Schedule1.DataBind()
    End Sub
    
    Sub lbAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
        FormView1.ChangeMode(FormViewMode.Insert)
    End Sub
    
    Protected Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertedEventArgs)
        If (Not e.Exception Is Nothing) Then
            lblError.Text = e.Exception.Message
            e.ExceptionHandled = True
            Return
        End If
        Schedule1.DataBind()
    End Sub

    Protected Sub FormView1_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs)
        If (Not e.Exception Is Nothing) Then
            lblError.Text = e.Exception.Message
            e.ExceptionHandled = True
            Return
        End If
        Schedule1.DataBind()
    End Sub

    Protected Sub FormView1_ItemDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewDeletedEventArgs)
        If (Not e.Exception Is Nothing) Then
            lblError.Text = e.Exception.Message
            e.ExceptionHandled = True
            Return
        End If
        Schedule1.DataBind()
    End Sub

    Protected Function RemoveDatePart(ByVal dt As DateTime) As DateTime
        ' MS Access uses 12/30/1899 as the date for short times
        Return New DateTime(1899, 12, 30, dt.Hour, dt.Minute, dt.Second)
    End Function

    Protected Sub FormView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs)
        ' Intercept update and remove date part from time fields
        e.NewValues("StartTime") = RemoveDatePart(e.NewValues("StartTime"))
        e.NewValues("EndTime") = RemoveDatePart(e.NewValues("EndTime"))
    End Sub
    
    Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs)
        ' Intercept insert and remove date part from time fields
        e.Values("StartTime") = RemoveDatePart(e.Values("StartTime"))
        e.Values("EndTime") = RemoveDatePart(e.Values("EndTime"))
    End Sub

    Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        If (FormView1.CurrentMode = FormViewMode.ReadOnly) Then
            Dim btnDelete As LinkButton = FormView1.FindControl("DeleteButton")
            If (btnDelete IsNot Nothing) Then
                btnDelete.Attributes.Add("onclick", "return confirm(""Are you sure?"")")
            End If
        End If
    End Sub
    
    Protected Sub Schedule1_EmptySlotClick(ByVal sender As Object, ByVal e As rw.ClickableTableCellEventArgs)
        ' prepare the FormView for inserting the selected slot
        FormView1.DefaultMode = FormViewMode.Insert
        If (FormView1.FindControl("StartTimeTextBox") Is Nothing) Then
            ' When there has been a postback already, and the FormView is not yet in insert mode,
            ' we end up here. As a consequence, in this case, the user needs to click twice 
            ' on the slot to add a new item.
            ' Any help with this problem is appreciated.
            FormView1.ChangeMode(FormViewMode.Insert)
            Return
        End If
        CType(FormView1.FindControl("StartTimeTextBox"), TextBox).Text = e.RangeStartValue
        CType(FormView1.FindControl("EndTimeTextBox"), TextBox).Text = e.RangeEndValue
        CType(FormView1.FindControl("EventDateTextBox"), TextBox).Text = CDate(e.Title).ToShortDateString()
        CType(FormView1.FindControl("TaskTextBox"), TextBox).Text = ""
    End Sub

    Protected Sub Schedule1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        FormView1.ChangeMode(FormViewMode.ReadOnly)
    End Sub
    
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Demo</title>
    <link href="css/demo1.css" type="text/css" rel="stylesheet" />
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
</head>
<body>
    <form id="Form1" runat="server">
        <rw:ScheduleCalendar id="Schedule1" runat="server" StartOfTimeScale="08:00:00" 
        ShowValueMarks="True" DataSourceID="AccessDataSource1" 
        EndOfTimeScale="12:00:00" TimeScaleInterval="30" BorderColor="Gray" 
        DateField="EventDate" StartTimeField="StartTime" EndTimeField="EndTime" 
        Layout="Vertical" GridLines="Both" NumberOfRepetitions="2" CellSpacing="0" 
        DataKeyNames="ID" HorizontalAlign="NotSet"  
        StartDay="Monday" EnableEmptySlotClick="True" 
        OnEmptySlotClick="Schedule1_EmptySlotClick" 
        OnSelectedIndexChanged="Schedule1_SelectedIndexChanged">
            <TimeTemplate>
                <%#Container.DataItem.ToShortTimeString()%> 
            </TimeTemplate>
            <DateStyle cssclass="title"></DateStyle>
            <DateTemplate>
                <%# CDate(Container.DataItem).ToString("ddd d/M/yyyy") %> 
            </DateTemplate>
            <BackgroundStyle cssclass="bground"></BackgroundStyle>
            <ItemStyle cssclass="item" Width="50px"></ItemStyle>
            <TimeStyle cssclass="rangeheader"></TimeStyle>
            <ItemTemplate>                 
                <asp:LinkButton id="LinkButton1" runat="Server" CommandName="Select" ><%#Eval("Task")%></asp:LinkButton>
            </ItemTemplate>
        </rw:ScheduleCalendar>
        <asp:AccessDataSource DataFile="~/App_Data/schedule.mdb" ID="AccessDataSource1" runat="server"
            SelectCommand="SELECT [ID], [StartTime], [EndTime], [EventDate], [Task] FROM [demo1]">
        </asp:AccessDataSource>
        <br />
        <table width="600">
            <tbody>
                <tr>
                    <td valign="top" width="50%">
                        <fieldset>
                            <legend>Properties</legend>Repeat
                            <asp:TextBox id="tbNumberOfRepetitions" runat="server" Width="41px" ToolTip="The number of repetitions to show at a time. Only used in Vertical layout.">2</asp:TextBox>x
                            <asp:TextBox ID="tbNumberOfDays" runat="server" Width="37px" ToolTip="The number of days to display. This number may be repeated multiple times in Vertical layout when the NumberOfRepetitions property is greater than 1.">7</asp:TextBox>days
                            <asp:Button id="btnRepetition" onclick="btnRepetition_Click" runat="server" Text="Set"></asp:Button>
                            <br />
                            Start on:
                            <asp:DropDownList ID="ddlStartDay" runat="server" OnSelectedIndexChanged="ddlStartDay_SelectedIndexChanged" AutoPostBack="True" ToolTip="The first day of the week to display. The calendar will start on this day of the week. This value is used only when the number of days equals 7." ></asp:DropDownList>
                            <br />
                            <asp:RadioButtonList id="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" RepeatColumns="2" RepeatLayout="Flow" RepeatDirection="Horizontal">
                                <asp:ListItem Value="Horizontal">Horizontal</asp:ListItem>
                                <asp:ListItem Value="Vertical" Selected="True">Vertical</asp:ListItem>
                            </asp:RadioButtonList>
                            <br />
                            <asp:CheckBox id="cbTimeScale" runat="server" Text="Full time scale" AutoPostBack="True" OnCheckedChanged="cbTimeScale_CheckedChanged"></asp:CheckBox>
                            <br />
                            Time scale interval: 
                            <asp:TextBox id="tbInterval" runat="server" Width="53px">30</asp:TextBox>
                            &nbsp;minutes 
                            <asp:Button id="btnApply" onclick="btnApply_Click" runat="server" Text="Apply" Enabled="False"></asp:Button>
                            <br />
                            <span style="WIDTH: 120px">Start of time scale:</span> 
                            <asp:TextBox id="tbStart" runat="server" Width="53px">8:00</asp:TextBox>
                            <br />
                            <span style="WIDTH: 120px">End of time scale:</span> 
                            <asp:TextBox id="tbEnd" runat="server" Width="53px">12:00</asp:TextBox>
                            <br />
                            <span style="WIDTH: 80px">Start date:</span> 
                            <asp:TextBox id="tbStartDate" runat="server" Width="100px"></asp:TextBox>
                            <asp:Button id="btnDate" onclick="btnDate_Click" runat="server" Text="Set date"></asp:Button>
                            <br />
                            <asp:CheckBox id="cbIncludeEnd" runat="server" Text="Include end time" AutoPostBack="True" OnCheckedChanged="cbIncludeEnd_CheckedChanged"></asp:CheckBox>
                             <br />
                            <asp:CheckBox id="cbValueMarks" runat="server" Text="Show value marks" AutoPostBack="True" OnCheckedChanged="cbValueMarks_CheckedChanged" Checked="True"></asp:CheckBox>
                        </fieldset>
                    </td>
                    <td width="50%">
                          <asp:LinkButton id="lbAdd" onclick="lbAdd_Click" runat="server">add new event</asp:LinkButton>&nbsp;&nbsp;
                          <asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" 
                              DataSourceID="AccessDataSource2"
                             OnItemInserted="FormView1_ItemInserted" 
                            OnItemUpdated="FormView1_ItemUpdated" 
                            OnItemDeleted="FormView1_ItemDeleted" 
                            OnItemUpdating="FormView1_ItemUpdating" 
                            OnItemInserting="FormView1_ItemInserting" 
                            OnDataBound="FormView1_DataBound">
                         <EditItemTemplate>
                            <fieldset>
                            <legend>Edit item</legend>
                                ID:
                               <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>'></asp:Label><br />
                               StartTime:
                               <asp:TextBox ID="StartTimeTextBox" runat="server" Text='<%# Bind("StartTime","{0:t}") %>'>
                               </asp:TextBox><br />
                               EndTime:
                               <asp:TextBox ID="EndTimeTextBox" runat="server" Text='<%# Bind("EndTime","{0:t}") %>'>
                               </asp:TextBox><br />
                               EventDate:
                               <asp:TextBox ID="EventDateTextBox" runat="server" Text='<%# Bind("EventDate","{0:d}") %>'>
                               </asp:TextBox><br />
                               Task:
                               <asp:TextBox ID="TaskTextBox" runat="server" Text='<%# Bind("Task") %>'>
                               </asp:TextBox><br />
                               <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                                   Text="Update">
                               </asp:LinkButton>
                               <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                                   Text="Cancel">
                               </asp:LinkButton>
                            </fieldset>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <fieldset>
                            <legend>Add item</legend>
                                StartTime:
                               <asp:TextBox ID="StartTimeTextBox" runat="server" Text='<%# Bind("StartTime","{0:t}") %>'>
                               </asp:TextBox><br />
                               EndTime:
                               <asp:TextBox ID="EndTimeTextBox" runat="server" Text='<%# Bind("EndTime","{0:t}") %>'>
                               </asp:TextBox><br />
                               EventDate:
                               <asp:TextBox ID="EventDateTextBox" runat="server" Text='<%# Bind("EventDate","{0:d}") %>'>
                               </asp:TextBox><br />
                               Task:
                               <asp:TextBox ID="TaskTextBox" runat="server" Text='<%# Bind("Task") %>'>
                               </asp:TextBox><br />
                               <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                                   Text="Insert">
                               </asp:LinkButton>
                               <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                                   Text="Cancel">
                               </asp:LinkButton>
                           </fieldset>
                       </InsertItemTemplate>
                       <ItemTemplate>
                         <fieldset>
                            <legend>Add item</legend>
                               ID:
                               <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>'></asp:Label><br />
                               StartTime:
                               <asp:Label ID="StartTimeLabel" runat="server" Text='<%# Bind("StartTime","{0:t}") %>'></asp:Label><br />
                               EndTime:
                               <asp:Label ID="EndTimeLabel" runat="server" Text='<%# Bind("EndTime","{0:t}") %>'></asp:Label><br />
                               EventDate:
                               <asp:Label ID="EventDateLabel" runat="server" Text='<%# Bind("EventDate","{0:d}") %>'></asp:Label><br />
                               Task:
                               <asp:Label ID="TaskLabel" runat="server" Text='<%# Bind("Task") %>'></asp:Label><br />
                               <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                                   Text="Edit">
                               </asp:LinkButton>
                               <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                                   Text="Delete">
                               </asp:LinkButton>
                               <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                                   Text="New">
                               </asp:LinkButton>
                                </fieldset>
                           </ItemTemplate>
                         </asp:FormView>
                        <asp:AccessDataSource ID="AccessDataSource2" runat="server" 
                        DataFile="~/App_Data/schedule.mdb"
                            SelectCommand="SELECT * FROM [demo1] WHERE ([ID] = ?)" 
                            DeleteCommand="DELETE FROM [demo1] WHERE [ID] = ?" 
                            InsertCommand="INSERT INTO [demo1] ([StartTime], [EndTime], [EventDate], [Task]) VALUES (?, ?, ?, ?)" 
                            UpdateCommand="UPDATE [demo1] SET [StartTime] = ?, [EndTime] = ?, [EventDate] = ?, [Task] = ? WHERE [ID] = ?">
                            <DeleteParameters>
                                <asp:Parameter Name="ID" Type="Int32" />
                            </DeleteParameters>
                            <UpdateParameters>
                                <asp:Parameter Name="StartTime" Type="DateTime" />
                                <asp:Parameter Name="EndTime" Type="DateTime" />
                                <asp:Parameter Name="EventDate" Type="DateTime" />
                                <asp:Parameter Name="Task" Type="String" />
                                <asp:Parameter Name="ID" Type="Int32" />
                            </UpdateParameters>
                            <SelectParameters>
                                <asp:ControlParameter ControlID="Schedule1" Name="ID" PropertyName="SelectedValue"
                                    Type="Int32" />
                            </SelectParameters>
                            <InsertParameters>
                                <asp:Parameter Name="StartTime" Type="DateTime" />
                                <asp:Parameter Name="EndTime" Type="DateTime" />
                                <asp:Parameter Name="EventDate" Type="DateTime" />
                                <asp:Parameter Name="Task" Type="String" />
                            </InsertParameters>
                        </asp:AccessDataSource>
                </tr>
            </tbody>
        </table>
    </form>
        <asp:Label ID="lblError" runat="server" EnableViewState="False" Forecolor="red" />
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Web Developer
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions