现在的位置: 首页 > 综合 > 正文

擴展 CommandField 類別 – 刪除提示訊息

2012年01月08日 ⁄ 综合 ⁄ 共 2850字 ⁄ 字号 评论关闭
GridView 中 CommandField 的刪除鈕預設是沒有刪除提示訊息,一般的作法是在 GridView 的 RowDataBound 事件中找到 CommandField 中的按鈕來設定它的刪除訊息。這種方式雖然可以達到需求,不過每次使用時都要自己增加程式碼是個麻煩的動作。

為了開發上的方便,本文中示範如何擴展 CommandField 類別,透過屬性就可以輕易設定刪除提示訊息。首先繼承 CommandField 下來命名為 TBCommandField,新增一個 DeleteConfirmMessage 屬性,用來設定刪除提示訊息;覆寫 InitializeCell 方法,找到按鈕並設定刪除提示訊息。

TBCommandField 類別完整的程式碼如下

 1 Imports System
 2 Imports System.Collections.Generic
 3 Imports System.ComponentModel
 4 Imports System.Text
 5 Imports System.Web
 6 Imports System.Web.UI
 7 Imports System.Web.UI.WebControls
 8 
 9 
10 Public Class TBCommandField
11     Inherits CommandField
12 
13     Private FDeleteConfirmMessage As String = String.Empty
14 
15     ''' <summary>
16     ''' 刪除詢問訊息。
17     ''' </summary>
18     Public Property DeleteConfirmMessage() As String
19         Get
20             Return FDeleteConfirmMessage
21         End Get
22         Set(ByVal value As String)
23             FDeleteConfirmMessage = value
24         End Set
25     End Property
26 
27     ''' <summary>
28     ''' 初始化儲存格。
29     ''' </summary>
30     ''' <param name="cell">儲存格。</param>
31     ''' <param name="cellType"></param>
32     ''' <param name="rowState"></param>
33     ''' <param name="rowIndex"></param>
34     ''' <remarks></remarks>
35     Public Overrides Sub InitializeCell(ByVal cell As DataControlFieldCell, ByVal cellType As DataControlCellType,
              ByVal
 rowState As DataControlRowState, ByVal rowIndex As Integer)
36         MyBase.InitializeCell(cell, cellType, rowState, rowIndex)
37         If Me.ShowDeleteButton AndAlso Me.Visible AndAlso Me.DeleteConfirmMessage <> String.Empty Then
38             SetButtonDeleteConfirm(cell)
39         End If
40     End Sub
41 
42     ''' <summary>
43     ''' 設定刪除鈕的刪除訊息。
44     ''' </summary>
45     ''' <param name="Cell">儲存格。</param>
46     Private Sub SetButtonDeleteConfirm(ByVal Cell As DataControlFieldCell)
47         Dim oControl As Control
48         Dim sScript As String
49 
50         sScript = "if (confirm('" & Me.DeleteConfirmMessage & "')==false) {return false;}"
51 
52         For Each oControl In Cell.Controls
53             If TypeOf (oControl) Is IButtonControl Then
54                 If DirectCast(oControl, IButtonControl).CommandName = "Delete" Then
55                     DirectCast(oControl, WebControl).Attributes("onclick"= sScript
56                     Exit Sub
57                 End If
58             End If
59         Next
60     End Sub
61 End Class
62 

上述程式碼中,是由 SetButtonDeleteConfirm 方法來設定刪除鈕的提示訊息。CommandField 的 ButtonType 可以為 Button、Link、Image 三者之一,依設定不同分別產生的命令鈕為 Button、LinkButton 或 ImageButton 三者之一,不過它們都具有 IButtonControl 介面,所以直接判斷 IButtonControl.CommandName 是否為 "Delete" 來判斷是否為刪除鈕,若是的話就直接設定其 Attributes("onclick") 加入刪除提示訊息。

使用 TBCommandField 的方式與 CommandField 相同,然後直接 aspx 程式碼中設定其 DeleteConfirmMessage 屬性就可以加入刪除提示訊息。

<bee:TBCommandField ShowDeleteButton="True" ShowEditButton="True" DeleteConfirmMessage="確定刪除嗎?" ButtonType="Button" />

抱歉!评论已关闭.