首先,你需要通過rightclicking在解決方案資源管理選項卡(我用的VWD)根文件創建一個.vb文件/類。選擇添加新,然後選擇班級頁面。它將提供創建App_Code文件夾,這是您的共享類將駐留的位置。將文件/類命名爲「DeleteButtonField.vb」,然後單擊確定。
然後它應該打開一個名爲DeleteButtonField的新.vb文件,您可以複製並粘貼或輸入下面的代碼。 (請注意,您可以使用智能感知完成的代碼很長的位定義了受保護的覆蓋子InitializeCell(........)。)
Imports Microsoft.VisualBasic
Imports System
Imports System.Web.UI.WebControls
Namespace myControls
Public Class DeleteButtonField
Inherits ButtonField
Private _confirmText As String = "Delete This Record?"
Public Property ConfirmText() As String
Get
Return _confirmText
End Get
Set(ByVal value As String)
_confirmText = value
End Set
End Property
Public Sub New()
Me.CommandName = "Delete"
Me.Text = "Delete"
End Sub
Public Overrides Sub InitializeCell(ByVal cell As System.Web.UI.WebControls.DataControlFieldCell, ByVal cellType As System.Web.UI.WebControl.DataControlCellType, ByVal rowState As System.Web.UI.WebControl.DataControlRowState, ByVal rowIndex As Integer)
MyBase.InitializeCell(cell, cellType, rowState, rowIndex)
If cellType = DataControlCellType.DataCell Then
Dim button As WebControl = CType(cell.Controls(0), WebControl)
button.Attributes("onclick") = String.Format("return confirm('{0}');", _confirmText)
End If
End Sub
End Class
End Namespace
保存.vb文件。然後在你的.aspx頁面中,在源代碼模式下打開頁面並找到你的GridView定義(即標籤),你可以選擇你想要刪除按鈕的位置,無論是第一個位置,第二個位置,確保你選擇文本位置,這樣你就不會改變任何的定義,並添加以下
<custom:DeleteButtonField ConfirmText="Are you sure that you want to delete this record?"></custom:DeleteButtonField>
你還需要在你的頁面的頂部的<%@頁後添加一行...>爲如下
<%@ Register TagPrefix="custom" Namespace="myControls" %>
這也需要你打算使用新的刪除按鈕在GridView的每一頁上添加有可能是其設置爲在web.config中默認的方式。我不是療法在我學習的這個階段。
保存您的.aspx頁面並進行測試。現在你已經定義了一個公共的Sub(它定義了一個標準的Delete按鈕及其行爲),你可以在你的應用程序中附加到任何GridView。
@abatishchev只是跟進,你有沒有得到這個解決? – Kelsey 2010-08-14 18:46:26
不,我放棄了,我的自定義刪除按鈕始終使用相同的硬編碼英文標籤 – abatishchev 2010-08-15 12:25:35