Click here to Skip to main content
15,886,091 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   633  
These data controls can show scheduled events automatically
<%@ Page Language="vb" %>
<%@ Register TagPrefix="rw" Namespace="rw" Assembly="Schedule2" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

    Function Helper1(o As Object) As String
        Dim thisDate As Date = CDate(o)
        Return WeekdayName(thisDate.DayOfWeek, vbSunday) & " " & thisDate.Day & "/" & thisDate.Month
    End Function

    Function Helper2(o As Object) As String
        Dim thisDate As Date = CDate(o)
        Return thisDate.ToShortTimeString()
    End Function

    Sub cbDateColumn_CheckedChanged(sender As Object, e As EventArgs)
        Schedule1.SeparateDateHeader = cbDateColumn.Checked
        Schedule1.DataBind()
    End Sub

    Sub RadioButtonList1_SelectedIndexChanged(sender As Object, e As EventArgs)
        If(RadioButtonList1.SelectedValue="Horizontal") Then
            Schedule1.Layout = LayoutEnum.Horizontal
        Else
            Schedule1.Layout = LayoutEnum.Vertical
        End If
        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 cbRangeMarks_CheckedChanged(sender As Object, e As System.EventArgs)
        Schedule1.ShowValueMarks  = cbRangeMarks.Checked
        Schedule1.DataBind()
    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

    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("BeginTime") = RemoveDatePart(e.NewValues("BeginTime"))
        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("BeginTime") = RemoveDatePart(e.Values("BeginTime"))
        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 btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        FormView1.ChangeMode(FormViewMode.Insert)
    End Sub
    
</script>
<html>
<head>
    <title>Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
    <link href="css/demo2.css" type="text/css" rel="stylesheet" />
</head>
<body>
    <form runat="server">
        <rw:ScheduleGeneral id="Schedule1" runat="server" GridLines="Both"
        DataRangeStartField="BeginTime" DataRangeEndField="EndTime"
        TitleField="Station" Layout="Horizontal" CellSpacing="0"
        ShowValueMarks="True" DataSourceID="AccessDataSource1" EndOfTimeScale="17:00:00" HorizontalAlign="NotSet" ItemStyleField="" StartOfTimeScale="08:00:00" DataKeyNames="ID" >
            <TitleStyle cssclass="heading"></TitleStyle>
            <ItemStyle cssclass="normal"></ItemStyle>
            <AlternatingItemStyle cssclass="normal2"></AlternatingItemStyle>
            <BackgroundStyle cssclass="empty"></BackgroundStyle>
            <RangeHeaderStyle cssclass="heading"></RangeHeaderStyle>
            <DateHeaderTemplate>
                <%# Helper1(Container.DataItem) %>
            </DateHeaderTemplate>
            <ItemTemplate>
                 <asp:LinkButton runat="server" CommandName="Select"><%#Eval("Program")%></asp:LinkButton>
            </ItemTemplate>
            <TitleTemplate>
                <%# Container.DataItem %>
            </TitleTemplate>
            <RangeHeaderTemplate>
                <%# Helper2(Container.DataItem) %>
            </RangeHeaderTemplate>
        </rw:ScheduleGeneral>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/schedule.mdb"
            SelectCommand="SELECT * FROM [demo3]"></asp:AccessDataSource>
        <br />
        <table width="600">
            <tbody>
                <tr>
                    <td valign="top" width="50%">
                        <fieldset>
                            <legend>Properties</legend>
                            <asp:CheckBox id="cbDateColumn" runat="server" Checked="False" OnCheckedChanged="cbDateColumn_CheckedChanged" AutoPostBack="True" Text="Header column for date"></asp:CheckBox>
                            <br />
                            <asp:RadioButtonList id="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" RepeatColumns="2" RepeatLayout="Flow" RepeatDirection="Horizontal">
                                <asp:ListItem Value="Horizontal" Selected="True">Horizontal</asp:ListItem>
                                <asp:ListItem Value="Vertical">Vertical</asp:ListItem>
                            </asp:RadioButtonList>
                            <br />
                            <asp:CheckBox id="cbTimeScale" runat="server" OnCheckedChanged="cbTimeScale_CheckedChanged" AutoPostBack="True" Text="Full time scale"></asp:CheckBox>
                            <br />
                            Time scale interval:
                            <asp:TextBox id="tbInterval" runat="server" Width="53px">10</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">17:00</asp:TextBox>
                            <br />
                            <span style="WIDTH: 120px">End of time scale:</span>
                            <asp:TextBox id="tbEnd" runat="server" Width="53px">23:00</asp:TextBox>
                            <br />
                            <asp:CheckBox id="cbRangeMarks" runat="server" Checked="True" Text="Show value marks" AutoPostBack="True" OnCheckedChanged="cbRangeMarks_CheckedChanged"></asp:CheckBox>
                        </fieldset>
                    </td>
                    <td valign="top" width="50%">
                         <asp:LinkButton ID="btnAdd" runat="server" OnClick="btnAdd_Click">add new item</asp:LinkButton>
                       <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 />
                                EventDate:
                                <asp:TextBox ID="EventDateTextBox" runat="server" Text='<%# Bind("EventDate","{0:d}") %>'>
                                </asp:TextBox><br />
                                BeginTime:
                                <asp:TextBox ID="BeginTimeTextBox" runat="server" Text='<%# Bind("BeginTime","{0:t}") %>'>
                                </asp:TextBox><br />
                                EndTime:
                                <asp:TextBox ID="EndTimeTextBox" runat="server" Text='<%# Bind("EndTime","{0:t}") %>'>
                                </asp:TextBox><br />
                                Station:
                                <asp:TextBox ID="StationTextBox" runat="server" Text='<%# Bind("Station") %>'>
                                </asp:TextBox><br />
                                Program:
                                <asp:TextBox ID="ProgramTextBox" runat="server" Text='<%# Bind("Program") %>'>
                                </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>
                               Event date:
                                <asp:TextBox ID="EventDateTextBox" runat="server" Text='<%# Bind("EventDate","{0:d}") %>'>
                                </asp:TextBox><br />
                                Begin time:
                                <asp:TextBox ID="BeginTimeTextBox" runat="server" Text='<%# Bind("BeginTime","{0:t}") %>'>
                                </asp:TextBox><br />
                                End time:
                                <asp:TextBox ID="EndTimeTextBox" runat="server" Text='<%# Bind("EndTime","{0:t}") %>'>
                                </asp:TextBox><br />
                                Station:
                                <asp:TextBox ID="StationTextBox" runat="server" Text='<%# Bind("Station") %>'>
                                </asp:TextBox><br />
                                Program:
                                <asp:TextBox ID="ProgramTextBox" runat="server" Text='<%# Bind("Program") %>'>
                                </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>Item</legend>
                               ID:
                                <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>'></asp:Label><br />
                                EventDate:
                                <asp:Label ID="EventDateLabel" runat="server" Text='<%# Bind("EventDate","{0:d}") %>'></asp:Label><br />
                                BeginTime:
                                <asp:Label ID="BeginTimeLabel" runat="server" Text='<%# Bind("BeginTime","{0:t}") %>'></asp:Label><br />
                                EndTime:
                                <asp:Label ID="EndTimeLabel" runat="server" Text='<%# Bind("EndTime","{0:t}") %>'></asp:Label><br />
                                Station:
                                <asp:Label ID="StationLabel" runat="server" Text='<%# Bind("Station") %>'></asp:Label><br />
                                Program:
                                <asp:Label ID="ProgramLabel" runat="server" Text='<%# Bind("Program") %>'></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"
                            DeleteCommand="DELETE FROM [demo3] WHERE [ID] = ?" 
                            InsertCommand="INSERT INTO [demo3] ([ID], [EventDate], [BeginTime], [EndTime], [Station], [Program]) VALUES (?, ?, ?, ?, ?, ?)"
                            SelectCommand="SELECT * FROM [demo3] WHERE ([ID] = ?)" 
                            UpdateCommand="UPDATE [demo3] SET [EventDate] = ?, [BeginTime] = ?, [EndTime] = ?, [Station] = ?, [Program] = ? WHERE [ID] = ?">
                            <DeleteParameters>
                                <asp:Parameter Name="ID" Type="Int32" />
                            </DeleteParameters>
                            <UpdateParameters>
                                <asp:Parameter Name="EventDate" Type="DateTime" />
                                <asp:Parameter Name="BeginTime" Type="DateTime" />
                                <asp:Parameter Name="EndTime" Type="DateTime" />
                                <asp:Parameter Name="Station" Type="String" />
                                <asp:Parameter Name="Program" 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="ID" Type="Int32" />
                                <asp:Parameter Name="EventDate" Type="DateTime" />
                                <asp:Parameter Name="BeginTime" Type="DateTime" />
                                <asp:Parameter Name="EndTime" Type="DateTime" />
                                <asp:Parameter Name="Station" Type="String" />
                                <asp:Parameter Name="Program" Type="String" />
                            </InsertParameters>
                        </asp:AccessDataSource>
                    </td>
                </tr>
            </tbody>
        </table>
        <asp:Label ID="lblError" runat="server" EnableViewState="False" Forecolor="red" />
    </form>
</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