Click here to Skip to main content
15,896,477 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" %>

<%@ Import Namespace="rw.Objects" %>

<%@ 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()
            Schedule1.DataBind()
        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 EventArgs)
        Schedule1.ShowValueMarks = cbValueMarks.Checked
        Schedule1.DataBind()
    End Sub
    
    Protected Sub ddlStartDay_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Schedule1.StartDay = CType(ddlStartDay.SelectedIndex, DayOfWeek)
        Schedule1.DataBind()
    End Sub
    
    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        FormView1.ChangeMode(FormViewMode.Insert)
    End Sub
    
    Protected Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As 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 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 FormViewDeletedEventArgs)
        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_DataBound(ByVal sender As Object, ByVal e As 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
    
</script>
<html>
<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 runat="server">
        <rw:ScheduleCalendar id="Schedule1" runat="server" CellPadding="1" 
        EndOfTimeScale="20:00:00" StartOfTimeScale="08:00:00" BorderColor="Gray" 
        DateField="EventDate" Layout="Vertical" GridLines="Both" NumberOfRepetitions="2" 
        TimeFieldsContainDate="True" StartTimeField="StartTime" EndTimeField="EndTime" 
        TimeFormatString="{0:t}" DateFormatString="{0:d}" DataKeyNames="ID"
        DataSourceID="AccessDataSource1" ItemStyleField="" 
        StartDate="2005-08-22" StartDay="Monday" HorizontalAlign="NotSet" ShowValueMarks="True">
            <DateStyle cssclass="title"></DateStyle>
            <BackgroundStyle cssclass="bground"></BackgroundStyle>
            <ItemStyle cssclass="item"></ItemStyle>
            <TimeStyle cssclass="rangeheader"></TimeStyle>
            <ItemTemplate>
              <asp:LinkButton id="LinkButton1" runat="Server" CommandName="Select"  ><%#Eval("Task")%></asp:LinkButton>
            </ItemTemplate>
            <TimeTemplate>
                <%#Container.DataItem.ToShortTimeString()%> 
            </TimeTemplate>
            <DateTemplate>
                <%#CDate(Container.DataItem).ToString("ddd d/M/yyyy")%> 
            </DateTemplate>
        </rw:ScheduleCalendar>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/schedule.mdb"
            SelectCommand="SELECT * FROM [demo4]"></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="btnAdd" onclick="btnAdd_Click" runat="server" 
                            Text="add new event"></asp:LinkButton>
                        <asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" 
                            DataSourceID="AccessDataSource2"
                            OnItemInserted="FormView1_ItemInserted" 
                            OnItemUpdated="FormView1_ItemUpdated" 
                            OnItemDeleted="FormView1_ItemDeleted" 
                            OnDataBound="FormView1_DataBound">
                            <EditItemTemplate>
                         <fieldset>
                            <legend>Edit item</legend>
                               ID:
                                <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>'></asp:Label><br />
                                Start date and time:
                                <asp:TextBox ID="StartTimeTextBox" runat="server" Text='<%# Bind("StartTime") %>'>
                                </asp:TextBox><br />
                                End date and time:
                                <asp:TextBox ID="EndTimeTextBox" runat="server" Text='<%# Bind("EndTime") %>'>
                                </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>
                                Start date and time:
                                <asp:TextBox ID="StartTimeTextBox" runat="server" Text='<%# Bind("StartTime") %>'>
                                </asp:TextBox><br />
                                End date and time:
                                <asp:TextBox ID="EndTimeTextBox" runat="server" Text='<%# Bind("EndTime") %>'>
                                </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>Item</legend>
                               ID:
                                <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>'></asp:Label><br />
                                Start date and time:
                                <asp:Label ID="StartTimeLabel" runat="server" Text='<%# Bind("StartTime") %>'></asp:Label><br />
                                EndTime:
                                <asp:Label ID="EndTimeLabel" runat="server" Text='<%# Bind("EndTime") %>'></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"
                            DeleteCommand="DELETE FROM [demo4] WHERE [ID] = ?" 
                            InsertCommand="INSERT INTO [demo4] ([StartTime], [EndTime], [Task]) VALUES (?, ?, ?)"
                            SelectCommand="SELECT * FROM [demo4] WHERE ([ID] = ?)" 
                            UpdateCommand="UPDATE [demo4] SET [StartTime] = ?, [EndTime] = ?, [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="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="Task" 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