【问题标题】:Custom GridView delete button自定义 GridView 删除按钮
【发布时间】:2011-01-28 08:06:38
【问题描述】:

如何自定义自动生成的命令按钮,例如Delete?

我想在删除时添加客户端确认,同时我希望在设置AutoGenerateDeleteButton="true" 时生成此按钮。可以吗??

我可以通过这种方式添加自定义按钮:

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

但不会自动本地化,也不会在设置AutoGenerateDeleteButton="true"时生成!

【问题讨论】:

    标签: .net asp.net javascript gridview


    【解决方案1】:

    我宁愿推荐使用 RowDataBound-event 而不是 PreRender-event。

    在那里,您可以轻松访问特定行中的元素。 (我认为 Kelsey 发布的解决方案可能存在分页问题(可能只是与 ajax 结合使用))

    给链接按钮一个 ID 并订阅 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";
          }
        }
      }
    

    【讨论】:

      【解决方案2】:

      您可以通过为网格实现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

      【讨论】:

      • @abatishchev 刚刚跟进,你解决了吗?
      • 不,我放弃了,我的自定义删除按钮始终是相同的硬编码英文标签
      【解决方案3】:

      首先,您需要通过右键单击“解决方案资源管理器”选项卡中的根文件来创建一个 .vb 文件/类(我使用 VWD)。选择添加新的并选择类页面。它将提供创建共享类所在的 App_Code 文件夹。将文件/类命名为“DeleteButtonField.vb”,然后单击“确定”。

      然后它应该会打开一个名为 DeleteButtonField 的新 .vb 文件,您可以复制和粘贴或输入以下代码。 (请注意,您可以使用 Intellisense 来完成定义 Protected Overrides Sub 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>
      

      您还需要在页面顶部的之后添加一行,如下所示

      &lt;%@ Register TagPrefix="custom" Namespace="myControls" %&gt; 这也需要添加到您打算在 GridView 中使用新删除按钮的每个页面上。可能有一种方法可以在 web.config 中将其设置为默认值;在我学习的这个阶段,我不在那里。

      保存您的 .aspx 页面并进行测试。您现在已经定义了一个通用的 Sub(它定义了一个标准的 Delete 按钮及其行为),您可以将其附加到应用程序中的任何 GridView。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-16
        • 2018-11-09
        • 2011-01-22
        • 1970-01-01
        • 2014-10-28
        • 2012-01-26
        相关资源
        最近更新 更多