摘要
之前在「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 事件的客户端指令码。
之前在「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
DayCommand 事件
''' <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
''' 月历控件。
''' </summary>
< _
Description("月历控件。"), _
ToolboxData("<{0}:TBCalendar runat=server></{0}:TBCalendar>") _
> _
Public Class TBCalendar
Inherits Calendar
DayCommand 事件
''' <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
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 事件。