一般 Calender 都是在 DayRender 事件中依需求加入子控制項,若有個需求要在 Calender 的每日的儲存格中加入一個按鈕,並希望按下這個按鈕能引發該按鈕的 Click 事件,此在事件中撰寫相關程式碼。

以上需求最直覺的方式就是新增一個 Button  加入 Cell 中,並使用 AddHandler 來設定 Click 事件的處理函式,程式碼如下。

 1     Protected Sub Calendar1_DayRender(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.DayRenderEventArgs)
           Handles
 Calendar1.DayRender
 2         Dim oButton As New Button()
 3         oButton.Text = "刪除"
 4         AddHandler oButton.Click, AddressOf Button_Click '設定 Button Click 處理函式
 5         e.Cell.Controls.Add(oButton)
 6     End Sub
 7 
 8     ''' <summary>
 9     ''' Button Click 事件的處理函式
10     ''' </summary>
11     Private Sub Button_Click(ByVal sender As ObjectByVal e As System.EventArgs)
12         '撰寫 Button Click 的程式碼
13     End Sub

可是上面的執行結果會跟你想像的不一樣,當 Button 產生 PostBack 並不會進入指定 Button_Click 方法。這是為什麼呢?因在 Page Load 事件中去查看 Calendar.Controls.Count 會是等於 0。由 PostBack  引發的控制項事件是去依 Request.Form  中資料跟控制項做比較決定是否引發相關事件,而該子控制項根本不存在,更不可能有機會引發它的 Click 事件了。

針對以上的問題,可以利用另一種方式來決定,就是按鈕直接呼叫 __doPostBack()  1     Protected Sub Calendar1_DayRender(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.DayRenderEventArgs)
           Handles
 Calendar1.DayRender
 2         Dim oButton As HtmlButton
 3 
 4         oButton = New HtmlButton()
 5         oButton.InnerText = "刪除"
 6         oButton.Attributes("onclick"= "__doPostBack('CalendarDelete','" & e.Day.Date.ToShortDateString() & "');"
 7         e.Cell.Controls.Add(oButton)
 8     End Sub
 9 
10     Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
11         If Me.Request.Form("__EVENTTARGET"= "CalendarDelete" Then
12             Dim oDate As Date
13             oDate = Date.Parse(Me.Request.Form("__EVENTARGUMENT"))
14             CalendarDelete(oDate)
15         End If
16 
17     End Sub
18 
19     ''' <summary>
20     ''' Calendar 刪除按鈕的處理方法。
21     ''' </summary>
22     ''' <param name="Value">日期。</param>
23     Private Sub CalendarDelete(ByVal Value As Date)
24         '撰寫刪除的相關程式碼
25     End Sub

相关文章:

  • 2021-06-30
  • 2021-12-04
  • 2021-12-03
  • 2022-12-23
  • 2022-02-26
  • 2022-01-12
  • 2022-12-23
  • 2021-06-04
猜你喜欢
  • 2021-11-14
  • 2021-09-12
  • 2021-07-09
  • 2022-12-23
  • 2021-06-19
  • 2022-02-14
  • 2022-12-23
相关资源
相似解决方案