擴展 CommandField 類別 - 刪除提示訊息含欄位值
在之前的「擴展 CommandField 類別 - 刪除提示訊息」文章中實作了 TBCommandField 類別,設定 DeleteConfirmMessage 屬性可以輕易設定刪除提示訊息。
這篇文章針對 TBCommandField 類別做進一步的擴展,我們希望在刪除訊息中可以加入指定的欄位值,讓使用者明確知道刪除的資料,例如刪除某個產品資料時,可以顯示這個產品的名稱在提示訊息中。
針對這個需求,在 TBCommandField 類別新增一個 DataField 屬性,設定刪除提示訊息中顯示欄位值的資料欄位名稱。因為要取得繫結的欄位資料,所以在 SetDeleteButton 私有方法中,設定刪除鈕 DataBinding 事件處理函式為 OnDataBindField,當 GridView 做 DataBind 時,就會引發子控制項的 DataBinding 事件,此時就會執行 OnDataBindField 方法,這時可以取得繫結的資料的 DataRowView,然後就可以從 DataRowView 中取得 DateField 的欄位值。
實作程式碼如下
001 Imports System
002 Imports System.Collections.Generic
003 Imports System.ComponentModel
004 Imports System.Text
005 Imports System.Web
006 Imports System.Web.UI
007 Imports System.Web.UI.WebControls
008
009
010 Public Class TBCommandField
011 Inherits CommandField
012
013 Private FDeleteConfirmMessage As String = String.Empty
014 Private FDataField As String = String.Empty
015
016 ''' <summary>
017 ''' 刪除提示訊息。
018 ''' </summary>
019 Public Property DeleteConfirmMessage() As String
020 Get
021 Return FDeleteConfirmMessage
022 End Get
023 Set(ByVal value As String)
024 FDeleteConfirmMessage = value
025 End Set
026 End Property
027
028 ''' <summary>
029 ''' 刪除提示訊息顯示欄位值的資料欄位名稱。
030 ''' </summary>
031 Public Property DataField() As String
032 Get
033 Return FDataField
034 End Get
035 Set(ByVal value As String)
036 FDataField = value
037 End Set
038 End Property
039
040 ''' <summary>
041 ''' 初始化儲存格。
042 ''' </summary>
043 ''' <param name="cell">要初始化的儲存格。</param>
044 ''' <param name="cellType">儲存格類型。</param>
045 ''' <param name="rowState">儲存格狀態。</param>
046 ''' <param name="rowIndex">資料列之以零起始的索引。</param>
047 ''' <remarks></remarks>
048 Public Overrides Sub InitializeCell(ByVal cell As DataControlFieldCell, ByVal cellType As DataControlCellType, ByVal rowState As DataControlRowState, ByVal rowIndex As Integer)
049 MyBase.InitializeCell(cell, cellType, rowState, rowIndex)
050 If Me.ShowDeleteButton AndAlso Me.Visible AndAlso Me.DeleteConfirmMessage <> String.Empty Then
051 SetDeleteButton(cell)
052 End If
053 End Sub
054
055 ''' <summary>
056 ''' 建立新的 TBCommandField 物件。
057 ''' </summary>
058 Protected Overrides Function CreateField() As DataControlField
059 Return New TBCommandField()
060 End Function
061
062 ''' <summary>
063 ''' 將目前 TBCommandField 物件的屬性複製到指定之 DataControlField 物件。
064 ''' </summary>
065 ''' <param name="newField">目的 DataControlField 物件。</param>
066 Protected Overrides Sub CopyProperties(ByVal NewField As DataControlField)
067 Dim oNewField As TBCommandField
068
069 oNewField = DirectCast(NewField, TBCommandField)
070 oNewField.DeleteConfirmMessage = Me.DeleteConfirmMessage
071 oNewField.DataField = Me.DataField
072 MyBase.CopyProperties(NewField)
073 End Sub
074
075 ''' <summary>
076 ''' 設定刪除鈕。
077 ''' </summary>
078 ''' <param name="Cell">儲存格。</param>
079 Private Sub SetDeleteButton(ByVal Cell As DataControlFieldCell)
080 Dim oControl As Control
081
082 For Each oControl In Cell.Controls
083 If TypeOf (oControl) Is IButtonControl Then
084 If DirectCast(oControl, IButtonControl).CommandName = "Delete" Then
085 '設定刪除鈕的 DataBinding 事件處理函式
086 AddHandler oControl.DataBinding, New EventHandler(AddressOf Me.OnDataBindField)
087 Exit Sub
088 End If
089 End If
090 Next
091 End Sub
092
093 ''' <summary>
094 ''' 將欄位值繫結至 TBCommandField 物件。
095 ''' </summary>
096 Protected Overridable Sub OnDataBindField(ByVal sender As Object, ByVal e As EventArgs)
097 Dim oControl As Control
098 Dim oDataItem As Object
099 Dim oDataRowView As Data.DataRowView
100 Dim sScript As String
101 Dim sMessage As String
102
103 oControl = DirectCast(sender, Control)
104
105 If Me.DataField <> String.Empty Then
106 '處理訊息中的欄位值
107 oDataItem = DataBinder.GetDataItem(oControl.NamingContainer)
108 oDataRowView = CType(oDataItem, Data.DataRowView)
109 sMessage = String.Format(Me.DeleteConfirmMessage, oDataRowView.Item(Me.DataField).ToString())
110 Else
111 sMessage = Me.DeleteConfirmMessage
112 End If
113
114 sMessage = Strings.Replace(sMessage, "'", "\'") '處理單引號
115 sScript = "if (confirm('" & sMessage & "')==false) {return false;}"
116 DirectCast(oControl, WebControl).Attributes("onclick") = sScript
117 End Sub
118
119 End Class
002 Imports System.Collections.Generic
003 Imports System.ComponentModel
004 Imports System.Text
005 Imports System.Web
006 Imports System.Web.UI
007 Imports System.Web.UI.WebControls
008
009
010 Public Class TBCommandField
011 Inherits CommandField
012
013 Private FDeleteConfirmMessage As String = String.Empty
014 Private FDataField As String = String.Empty
015
016 ''' <summary>
017 ''' 刪除提示訊息。
018 ''' </summary>
019 Public Property DeleteConfirmMessage() As String
020 Get
021 Return FDeleteConfirmMessage
022 End Get
023 Set(ByVal value As String)
024 FDeleteConfirmMessage = value
025 End Set
026 End Property
027
028 ''' <summary>
029 ''' 刪除提示訊息顯示欄位值的資料欄位名稱。
030 ''' </summary>
031 Public Property DataField() As String
032 Get
033 Return FDataField
034 End Get
035 Set(ByVal value As String)
036 FDataField = value
037 End Set
038 End Property
039
040 ''' <summary>
041 ''' 初始化儲存格。
042 ''' </summary>
043 ''' <param name="cell">要初始化的儲存格。</param>
044 ''' <param name="cellType">儲存格類型。</param>
045 ''' <param name="rowState">儲存格狀態。</param>
046 ''' <param name="rowIndex">資料列之以零起始的索引。</param>
047 ''' <remarks></remarks>
048 Public Overrides Sub InitializeCell(ByVal cell As DataControlFieldCell, ByVal cellType As DataControlCellType, ByVal rowState As DataControlRowState, ByVal rowIndex As Integer)
049 MyBase.InitializeCell(cell, cellType, rowState, rowIndex)
050 If Me.ShowDeleteButton AndAlso Me.Visible AndAlso Me.DeleteConfirmMessage <> String.Empty Then
051 SetDeleteButton(cell)
052 End If
053 End Sub
054
055 ''' <summary>
056 ''' 建立新的 TBCommandField 物件。
057 ''' </summary>
058 Protected Overrides Function CreateField() As DataControlField
059 Return New TBCommandField()
060 End Function
061
062 ''' <summary>
063 ''' 將目前 TBCommandField 物件的屬性複製到指定之 DataControlField 物件。
064 ''' </summary>
065 ''' <param name="newField">目的 DataControlField 物件。</param>
066 Protected Overrides Sub CopyProperties(ByVal NewField As DataControlField)
067 Dim oNewField As TBCommandField
068
069 oNewField = DirectCast(NewField, TBCommandField)
070 oNewField.DeleteConfirmMessage = Me.DeleteConfirmMessage
071 oNewField.DataField = Me.DataField
072 MyBase.CopyProperties(NewField)
073 End Sub
074
075 ''' <summary>
076 ''' 設定刪除鈕。
077 ''' </summary>
078 ''' <param name="Cell">儲存格。</param>
079 Private Sub SetDeleteButton(ByVal Cell As DataControlFieldCell)
080 Dim oControl As Control
081
082 For Each oControl In Cell.Controls
083 If TypeOf (oControl) Is IButtonControl Then
084 If DirectCast(oControl, IButtonControl).CommandName = "Delete" Then
085 '設定刪除鈕的 DataBinding 事件處理函式
086 AddHandler oControl.DataBinding, New EventHandler(AddressOf Me.OnDataBindField)
087 Exit Sub
088 End If
089 End If
090 Next
091 End Sub
092
093 ''' <summary>
094 ''' 將欄位值繫結至 TBCommandField 物件。
095 ''' </summary>
096 Protected Overridable Sub OnDataBindField(ByVal sender As Object, ByVal e As EventArgs)
097 Dim oControl As Control
098 Dim oDataItem As Object
099 Dim oDataRowView As Data.DataRowView
100 Dim sScript As String
101 Dim sMessage As String
102
103 oControl = DirectCast(sender, Control)
104
105 If Me.DataField <> String.Empty Then
106 '處理訊息中的欄位值
107 oDataItem = DataBinder.GetDataItem(oControl.NamingContainer)
108 oDataRowView = CType(oDataItem, Data.DataRowView)
109 sMessage = String.Format(Me.DeleteConfirmMessage, oDataRowView.Item(Me.DataField).ToString())
110 Else
111 sMessage = Me.DeleteConfirmMessage
112 End If
113
114 sMessage = Strings.Replace(sMessage, "'", "\'") '處理單引號
115 sScript = "if (confirm('" & sMessage & "')==false) {return false;}"
116 DirectCast(oControl, WebControl).Attributes("onclick") = sScript
117 End Sub
118
119 End Class
使用時在 aspx 程式碼中設定其 DeleteConfirmMessage 及 DateField 屬性就在刪除提示訊息加入欄位值。其中 DeleteConfirmMessage="確定刪除 {0} 這筆資料嗎?" 中的 {0} 就是 ProductName 欄位值顯示的位置。
<bee:TBCommandField ShowDeleteButton="True" ShowEditButton="True" DataField="ProductName" DeleteConfirmMessage="確定刪除 {0} 這筆資料嗎?" ButtonType="Button" />>
執行結果如下