扩展 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 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 ObjectByVal 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 StringByVal [DateAs DateAs 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 ObjectByVal 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 ObjectByVal 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 事件。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值