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" />

擴展 CommandField 類別 - 刪除提示訊息相关推荐

  1. php class variable,PHP中的變量類擴展 - 是否可能?

    Is something like the following possible in PHP? 在PHP中可能會出現以下內容嗎? $blah = 'foo1'; class foo2 extends ...

  2. ReportViewer 類別

    封裝用於 ReportViewer 控制項的方法和屬性. 繼承階層架構 Object   Control     WebControl       CompositeControl         M ...

  3. java implements t_Java泛型——為什么“擴展T”允許而不是“實現T”?

    I wonder if there is a special reason in Java for using always "extends" rather than " ...

  4. [Java] 基本資料包裝類別 Wrapper Classes

    基本型別包裝 (Wrapper Classes) 將基本型別生成物件,要將基本型別先包裝成物件,才能執行生成, Boxing: Integer a = new Integer(1) Unboxing: ...

  5. BAT批量重命名文件擴展名

    ::rem 重命名文件 ::.txt轉為.sql for /f "delims=" %%i in ('dir /b /a-d /s "*.txt"') do r ...

  6. C++ 字元陣列(C-style)、字元指標、String類別 使用方式整理

    (一)字元陣列(C-style)的宣告與使用: 第一種:char str[] = {'h','e','l','l','o','\0]}; 第二種:char str[] = "hello&qu ...

  7. 老雷PHP教程,老雷socket編程之PHP利用socket擴展實現聊天服務

    老雷socket編程之PHP利用socket擴展實現聊天服務 socket聊天服務原理 PHP有兩個socket的擴展 sockets和streams sockets socket_create(AF ...

  8. CultureInfo 類別

    CultureInfo 類別 .NET Framework 2.0 其他版本 .NET Framework 4.5 .NET Framework 4 .NET Framework 3.5 Silver ...

  9. Bootstrap笔记(十二) 常用類別 - 色彩

    常用類別 - 色彩 文字色彩 背景色彩 文字色彩 Bootstrap提供以下類別用來設定文字色彩 : 範例: 文字色彩 不同的文字色彩類別會呈現不同的文字色彩,其中最後一行在 .text-black ...

最新文章

  1. 百度也出分享(百度分享)
  2. CentOS7安装和配置samba
  3. 2017.9.5 postgresql加密函数的使用
  4. 【实践驱动开发3-005】TI WL1835MODCOM8 在android的移植 - SDIO and wifi 基础
  5. java.sql.SQLException: Io 异常: The Network Adapter could not establish the connection 解决
  6. kuangbin专题-平整数组
  7. x10I pC套件 官方网站下载
  8. Halcon教程九:把Halcon程序放到C#程序里
  9. 微信小程序 input输入事件
  10. html ur是什么意思_url是什么意思?
  11. 「数据架构」数据模型,数据字典,数据库模式 和ERD的比较
  12. ‘parent.relativePath‘ points at com.xxx instead of org.springframework.boot:spring-boot-starter的快速解决
  13. APP测试基础--小工具介绍(1)
  14. 盯住Z世代增量,汽车之家818车晚透露哪些营销信号?
  15. 前端学习记录005_canvas绘制钟表
  16. 向下兼容性格什么意思_恭喜你,被向下兼容了|有启发
  17. 2018-07-13心情日记
  18. Qt编写自定义控件:高亮滑动导航菜单
  19. 《笨兔兔的故事》之文件系统部分读书心得
  20. 【Codeforces Round #172】Codeforces 280D k-Maximum Subsequence Sum

热门文章

  1. python设计模式9-装饰器模式
  2. @Resource注解使用详解
  3. 关于Kafka高性能的几个问题
  4. 使用jfreechart来创建一个简单的柱状图
  5. Grace Ex助力区块链数字资产迈向全新未来
  6. 黄聪:PHP去掉转义后字符串中的反斜杠\函数stripslashes
  7. 第一款Micropython图形化编辑器—Python Editor
  8. erlang的cpu调优
  9. mysql数据修改-DEDE
  10. 宽容随和 不失勤恳 充满信心--对工作、生活的一些感悟