继承 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 TBCalendarClass TBCalendar
Inherits Calendar
DayCommand 事件#Region " DayCommand 事件 "
/**/''' <summary>
''' DayCommand 事件自变量。
''' </summary>
Public Class DayCommandEventArgsClass DayCommandEventArgs
Inherits System.EventArgs
Private FCommandName As String = String.Empty
Private FDate As Date = Date.MinValue
/**/''' <summary>
''' 命令名称。
''' </summary>
Public Property CommandName()Property CommandName() As String
Get
Return FCommandName
End Get
Set(ByVal value As String)
FCommandName = value
End Set
End Property
/**/''' <summary>
''' 日期。
''' </summary>
Public Property [()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()Sub OnDayCommand(ByVal e As DayCommandEventArgs)
RaiseEvent DayCommand(Me, e)
End Sub
#End Region
/**/''' <summary>
''' 引发 PostBack 产生的控件事件。
''' </summary>
Protected Overrides Sub RaisePostBackEvent()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()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()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()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 事件。
