摘要
延續前面「GridView+FormView 示範資料 新增/修改/刪除(進階篇:伺服器控制項)」的文章,文章後記有提及若要達到零程式碼要求,其中一個要件就是需要擴展 CommandField 類別,在 CommandField 的 Header 的部分加入「新增」鈕,本文就是在說明如何擴展 CommandField 類別達到此需求。
擴展 CommandField 類別
我們的需求是在 CommandField 的 Header 加入新增鈕,作法是繼承 CommandField 下來命名為 TBCommandField,新增 ShowHeaderInsertButton 屬性,來設定 Header 是否顯示新增鈕。TBCommandField 的程式碼如下,作法是覆寫 InitializeCell 方法,若為 Header 且有設定 ShowHeaderInsertButton="True" 則利用 AddButtonToCell 私有方法來加入「新增」鈕。
- Imports System
- Imports System.Collections.Generic
- Imports System.ComponentModel
- Imports System.Text
- Imports System.Web
- Imports System.Web.UI
- Imports System.Web.UI.WebControls
- Imports System.Globalization
- Namespace WebControls
- Public Class TBCommandField
- Inherits CommandField
- Private FShowHeaderInsertButton As Boolean = False
- '''
- ''' 初始化儲存格。
- '''
- ''' 要初始化的儲存格。
- ''' 儲存格類型。
- ''' 儲存格狀態。
- ''' 資料列之以零起始的索引。
- Public Overrides Sub InitializeCell(ByVal cell As DataControlFieldCell, ByVal cellType As DataControlCellType, ByVal rowState As DataControlRowState, ByVal rowIndex As Integer)
- MyBase.InitializeCell(cell, cellType, rowState, rowIndex)
- If Me.ShowHeaderInsertButton AndAlso (cellType = DataControlCellType.Header) Then
- '標題加入新增鈕
- AddButtonToCell(cell, "New", Me.InsertText, Me.CausesValidation, Me.ValidationGroup, rowIndex, Me.InsertImageUrl)
- End If
- End Sub
- '''
- ''' 標題儲存格是否顯示新增鈕。
- '''
- < _
- Description("標題儲存格是否顯示新增鈕"), _
- DefaultValue(False) _
- > _
- Public Property ShowHeaderInsertButton() As Boolean
- Get
- Return FShowHeaderInsertButton
- End Get
- Set(ByVal value As Boolean)
- If FShowHeaderInsertButton <> value Then
- FShowHeaderInsertButton = value
- Me.OnFieldChanged()
- End If
- End Set
- End Property
- '''
- ''' 儲存格加入按鈕。
- '''
- ''' 儲存格。
- ''' 按鈕命令。
- ''' 按鈕文字。
- ''' 是否執行驗證。
- ''' 驗證控制項所屬之驗證群組的名稱。
- ''' 列索引。
- ''' 影像網址。
- Private Sub AddButtonToCell(ByVal Cell As DataControlFieldCell, ByVal CommandName As String, ByVal ButtonText As String, ByVal CausesValidation As Boolean, ByVal ValidationGroup As String, ByVal RowIndex As Integer, ByVal ImageUrl As String)
- Dim oButtonControl As IButtonControl
- Select Case Me.ButtonType
- Case ButtonType.Button
- oButtonControl = New Button()
- Exit Select
- Case ButtonType.Link
- oButtonControl = New LinkButton()
- Case Else
- oButtonControl = New ImageButton()
- DirectCast(oButtonControl, ImageButton).ImageUrl = ImageUrl
- Exit Select
- End Select
- oButtonControl.Text = ButtonText
- oButtonControl.CommandName = CommandName
- oButtonControl.CommandArgument = RowIndex.ToString(CultureInfo.InvariantCulture)
- oButtonControl.CausesValidation = CausesValidation
- oButtonControl.ValidationGroup = ValidationGroup
- Cell.Controls.Add(DirectCast(oButtonControl, WebControl))
- End Sub
- '''
- ''' 建立新的 TBCommandField 物件。
- '''
- Protected Overrides Function CreateField() As DataControlField
- Return New TBCommandField()
- End Function
- '''
- ''' 將目前 TBCommandField 物件的屬性複製到指定之 DataControlField 物件。
- '''
- ''' 目的 DataControlField 物件。
- Protected Overrides Sub CopyProperties(ByVal NewField As DataControlField)
- Dim oNewField As TBCommandField
- oNewField = DirectCast(NewField, TBCommandField)
- oNewField.ShowHeaderInsertButton = Me.ShowHeaderInsertButton
- MyBase.CopyProperties(NewField)
- End Sub
- End Class
- End Namespace
使用 TBCommandField 類別
因為 GridView.Columns 的屬性編輯器不支援我們改寫 TBCommandField,故只能切換至 aspx 程式碼中手動加入。
- < bee:TBCommandField ShowHeaderInsertButton="True" InsertText="新增" ShowEditButton="True" ShowDeleteButton="True" ButtonType="Button" />
切換至設計畫面,就可以看到 TBCommandField 的 Header 出現「新增」鈕。
備註:若要讓 GridView 的欄位編輯器支援 TBCommandField,需自訂 GridView.Columns 的屬性編輯器。
