2010-03-16 147 views
1

如何自定義自動生成的命令按鈕,例如Delete自定義GridView刪除按鈕

我想添加一個客戶端刪除的確認,並在同一時刻,我想這個按鈕將生成設置AutoGenerateDeleteButton="true"。可能嗎??

我可以添加自定義按鈕是這樣的:

<asp:TemplateField> 
    <ItemTemplate> 
     <asp:LinkButton runat="server" CommandName="Delete" OnClientClick="return confirm('Delete?')">Delete</asp:LinkButton> 
    </ItemTemplate> 
</asp:TemplateField> 

但將不會自動定位,並會在設置AutoGenerateDeleteButton="true"將不會產生!

回答

0

您可以通過爲網格實施PreRender事件來做到這一點。

下面是一些基本的僞代碼:

protected void yourGrid_PreRender(object sender, EventArgs e) 
{ 
    GridView grd = (GridView)(sender); 

    // iterate through all your rows and look for the button 
    // make sure to add code to verify your rows, columns, and control bounds are valid 
    for (int rowIndex = 0; rowIndex < grd.Rows.Count; rowIndex++) 
    { 
     LinkButton btn = grd.Rows[rowIndex].Cells[deleteButtonColumnIndex].Controls[0] as LinkButton; 

     // Here you have access to the button so change it to do what you need. 
     btn.OnClientClick = string.Format("return confirm('{0}?')", btn.Text); 
    } 
} 

另外如果你想它烤你可能需要擴展GridView和執行自己的代碼。請參閱以下主題:

http://forums.asp.net/p/1396268/3011988.aspx#3011988

+0

@abatishchev只是跟進,你有沒有得到這個解決? – Kelsey 2010-08-14 18:46:26

+0

不,我放棄了,我的自定義刪除按鈕始終使用相同的硬編碼英文標籤 – abatishchev 2010-08-15 12:25:35

2

我寧願推薦使用的RowDataBound事件,而不是預先渲染事件。

在那裏您可以輕鬆訪問特定行中的元素。 (我認爲Kelsey發佈的解決方案可能會遇到尋呼問題(可能只是與ajax結合))

給Linkbutton一個ID和subsribe給RowDataBound事件。

void gv_RowDataBound(Object sender, GridViewRowEventArgs e) 
    { 
    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     LinkButton _foo = e.Row.FindControl("LINKBUTTONID") as LinkButton; 
     if(_foo != null) 
     { 
     _foo.OnClientClick = "insert localized text here"; 
     } 
    } 
    } 
0

首先,你需要通過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。