擴展 Calendar 控制項 - DayCommand 事件

擴展 Calendar 控制項 - DayCommand 事件

摘要

之前在「Calendar 動態產生子控制項的 Event Handler」一文中有提到如何處理 Calendar 在 DayRender 事件動態產生按鈕的事件處理,文中最後有提及,若 Calendar 控制項有類似 GridView 控制項中有 RowCommand 事件,在使用上就可以更簡化。所以本文將擴展 Calendar 控制項,新增 DayCommand 事件,就動態產生的按鈕可以可以引發 DayCommand 事件,以便做後序的命令處理。

 

擴展 Calendar 控制項

繼承 Calendar 命名為 TBCalendar,新增 DayCommand 事件,覆寫 RaisePostBackEvent 方法,此方法是在處理引發 PostBack 產生的控制項事件,在此判斷若 PostBack 傳入的引數為 DayCommand${0}${1} 格式 (其中 {0} 為 CommandName,{1} 為日期),則引發 DayCommand 事件。另外新增 GetDayCommandEventReference 方法,提供取得引發 DayCommand 事件的用戶端指令碼。


    ''' <summary>
    ''' 月曆控制項。
    ''' </summary>
    < _
    Description("月曆控制項。"), _
    ToolboxData("<{0}:TBCalendar runat=server></{0}:TBCalendar>") _
    > _
    Public Class TBCalendar
        Inherits Calendar

#Region " DayCommand 事件 "

        ''' <summary>
        ''' DayCommand 事件引數。
        ''' </summary>
        Public Class DayCommandEventArgs
            Inherits System.EventArgs
            Private FCommandName As String = String.Empty
            Private FDate As Date = Date.MinValue

            ''' <summary>
            ''' 命令名稱。
            ''' </summary>
            Public Property CommandName() As String
                Get
                    Return FCommandName
                End Get
                Set(ByVal value As String)
                    FCommandName = value
                End Set
            End Property

            ''' <summary>
            ''' 日期。
            ''' </summary>
            Public Property [Date]() As Date
                Get
                    Return FDate
                End Get
                Set(ByVal value As Date)
                    FDate = value
                End Set
            End Property

        End Class

        ''' <summary>
        ''' 日期儲存格引發的命令事件。
        ''' </summary>
        < _
        System.ComponentModel.Category(WebCommon.Category.Action), _
        System.ComponentModel.Description("日期儲存格引發的命令事件。") _
        > _
        Public Event DayCommand(ByVal sender As Object, ByVal e As DayCommandEventArgs)

        ''' <summary>
        ''' 引發 DayCommand 事件。
        ''' </summary>
        Protected Overridable Sub OnDayCommand(ByVal e As DayCommandEventArgs)
            RaiseEvent DayCommand(Me, e)
        End Sub

#End Region

        ''' <summary>
        ''' 引發 PostBack 產生的控制項事件。
        ''' </summary>
        Protected Overrides Sub RaisePostBackEvent(ByVal eventArgument As String)
            Dim oArgument() As String
            Dim oEventArgs As DayCommandEventArgs

            If String.Compare(eventArgument, 0, "DayCommand", 0, "DayCommand".Length, StringComparison.Ordinal) = 0 Then

                oArgument = Split(eventArgument, "$")
                If oArgument.Length <> 3 Then Exit Sub
                oEventArgs = New DayCommandEventArgs()
                oEventArgs.CommandName = oArgument(1)
                oEventArgs.Date = Date.Parse(oArgument(2))
                OnDayCommand(oEventArgs)
                Exit Sub
            End If

            MyBase.RaisePostBackEvent(eventArgument)
        End Sub

        ''' <summary>
        ''' 取得引發 DayCommand 事件的用戶端指令碼。
        ''' </summary>
        ''' <param name="CommandName">命令名稱。</param>
        ''' <param name="Date">日期。</param>
        Public Function GetDayCommandEventReference(ByVal CommandName As String, ByVal [Date] As Date) As String
            Dim sArgument As String

            sArgument = String.Format("DayCommand${0}${1}", CommandName, [Date].ToShortDateString)
            Return Me.Page.ClientScript.GetPostBackEventReference(Me, sArgument)
        End Function


    End Class


測試程式

在頁面上放置 TBCalendar 控制項,在 DayRender 事件中動態產生一個 HtmlButton 按鈕,並利用 GetDayCommandEventReference 方法取得引發 DayCommand 事件的用戶端指令碼。在 DayCommand 事件中將 e.CommandName 及 e.Date 輸出在頁面上。

 


    Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
        Dim oButton As HtmlButton

        oButton = New HtmlButton()
        oButton.InnerText = "刪除"
        oButton.Attributes("onclick") = Calendar1.GetDayCommandEventReference("Delete", e.Day.Date)
        e.Cell.Controls.Add(oButton)
    End Sub

    Protected Sub Calendar1_DayCommand(ByVal sender As Object, ByVal e As Bee.Web.WebControls.TBCalendar.DayCommandEventArgs) Handles Calendar1.DayCommand
        Me.Response.Write(String.Format("DayCommand: {0} Date: {1}", e.CommandName, e.Date.ToShortDateString))
    End Sub

執行程式,按下某一日期的 [刪除] 鈕,就會引發其對應的 DayCommand 事件。

image

ASP.NET 魔法學院