Click here to Skip to main content
15,893,266 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
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports rw

Public Class demo4
    Inherits System.Web.UI.Page

    Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Page.Server.MapPath("App_Data\schedule.mdb")

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Schedule1 As ScheduleCalendar
    Protected WithEvents txtWeeks As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnWeeks As System.Web.UI.WebControls.Button
    Protected WithEvents RadioButtonList1 As System.Web.UI.WebControls.RadioButtonList
    Protected WithEvents cbTimeScale As System.Web.UI.WebControls.CheckBox
    Protected WithEvents tbInterval As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnApply As System.Web.UI.WebControls.Button
    Protected WithEvents tbStart As System.Web.UI.WebControls.TextBox
    Protected WithEvents tbEnd As System.Web.UI.WebControls.TextBox
    Protected WithEvents tbStartDate As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnDate As System.Web.UI.WebControls.Button
    Protected WithEvents cbIncludeEnd As System.Web.UI.WebControls.CheckBox
    Protected WithEvents cbShowValueMarks As System.Web.UI.WebControls.CheckBox
    Protected WithEvents txtStart As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtEnd As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtTask As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnAdd As System.Web.UI.WebControls.Button
    Protected WithEvents LinkButton1 As System.Web.UI.WebControls.LinkButton

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not (IsPostBack) Then
            ' 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()
            BindSchedule()
        End If
    End Sub
    Sub BindSchedule()

        Dim strSQL As String = "SELECT ID, StartTime, EndTime, Task FROM demo4;"
        ' For performance reasons, it would be a good idea to limit the query to
        ' a range of dates only ( SELECT ... WHERE EventDate BETWEEN ? AND ? )

        Dim cn As New OleDbConnection(strConn)
        Dim cm As New OleDbCommand(strSQL, cn)
        Try
            cn.Open()
            Dim da As New OleDbDataAdapter(cm)
            Dim ds As New DataSet
            da.Fill(ds)
            Schedule1.DataSource = ds
            Schedule1.DataBind()
        Finally
            cn.Close()
        End Try

    End Sub

    Sub btnWeeks_Click(ByVal sender As Object, ByVal e As EventArgs)
        Schedule1.Weeks = CInt(txtWeeks.Text)
        BindSchedule()
    End Sub

    Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim strSQL As String = "INSERT INTO demo4 (StartTime, EndTime, Task) " & _
            "VALUES (@StartTime,@EndTime,@Task);"

        Dim cn As New OleDbConnection(strConn)
        Dim cm As New OleDbCommand(strSQL, cn)
        Try
            cn.Open()
            cm.Parameters.Add("@StartTime", txtStart.Text)
            cm.Parameters.Add("@EndTime", txtEnd.Text)
            cm.Parameters.Add("@Task", txtTask.Text)
            cm.ExecuteNonQuery()
        Catch ex As Exception
            Trace.Warn(ex.Message)
        Finally
            cn.Close()
        End Try
        BindSchedule()
    End Sub

    Function Helper(ByVal obj As Object) As String
        ' Helper function to format the date in the header
        ' obj should be of type Date
        Dim thisDate As Date = CDate(obj)
        Return thisDate.ToString("dddd") & " " & thisDate.ToShortDateString()
    End Function

    Sub Schedule1_ItemCommand(ByVal sender As Object, ByVal e As ScheduleCommandEventArgs)
        If e.CommandName = "Delete" Then
            Dim strSQL As String = "DELETE FROM demo4 WHERE ID=@ID;"

            Dim cn As New OleDbConnection(strConn)
            Dim cm As New OleDbCommand(strSQL, cn)
            Try
                cn.Open()
                ' The Schedule control doesn't have a DataKeys member (yet),
                ' so we use a workaround with CommandArgument
                cm.Parameters.Add("@ID", e.CommandArgument)
                cm.ExecuteNonQuery()
            Catch ex As Exception
                Trace.Warn(ex.Message)
            Finally
                cn.Close()
            End Try
            BindSchedule()
        End If
    End Sub

    Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        If (RadioButtonList1.SelectedValue = "Horizontal") Then
            Schedule1.Layout = LayoutEnum.Horizontal
        Else
            Schedule1.Layout = LayoutEnum.Vertical
        End If
        BindSchedule()
    End Sub

    Sub cbIncludeEnd_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Schedule1.IncludeEndValue = cbIncludeEnd.Checked
        If(cbIncludeEnd.Checked) Then 
            cbShowValueMarks.Checked = False ' range marks don't work when IncludeEndValue=True
            Schedule1.ShowValueMarks = False
        End If
        BindSchedule()
    End Sub

    Sub cbShowValueMarks_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Schedule1.ShowValueMarks  = cbShowValueMarks.Checked
        If(cbShowValueMarks.Checked) Then 
            cbIncludeEnd.Checked = False ' range marks don't work when IncludeEndValue=True
            Schedule1.IncludeEndValue = False
        End If
        BindSchedule()
    End Sub

    Sub cbTimeScale_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Schedule1.FullTimeScale = cbTimeScale.Checked
        btnApply.Enabled = cbTimeScale.Checked
        BindSchedule()
    End Sub

    Sub btnApply_Click(ByVal sender As Object, ByVal e As EventArgs)
        Schedule1.TimeScaleInterval = CInt(tbInterval.Text)
        Schedule1.StartOfTimeScale = TimeSpan.Parse(tbStart.Text)
        Schedule1.EndOfTimeScale = TimeSpan.Parse(tbEnd.Text)
        BindSchedule()
    End Sub

    Sub btnDate_Click(ByVal sender As Object, ByVal e As EventArgs)
        Schedule1.StartDate = DateTime.Parse(tbStartDate.Text)
        BindSchedule()
    End Sub
End Class

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