Custom GridView Delete Button

How to configure an automatically generated command button, for example. Delete?

I want to add client confirmation when uninstalling, and at the same time I want this button to be generated during configuration AutoGenerateDeleteButton="true". Is it possible?

I can add a custom button as follows:

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

but it will not be automatically localized and will not be created during configuration AutoGenerateDeleteButton="true"!

+1
source share
3 answers

I would rather use the RowDataBound event instead of the PreRender event.

. ( , , Kelsey, (, ajax))

Linkbutton 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 .

psuedo:

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

.vb/class, " " ( VWD). " " "". App_Code, . / "DeleteButtonField.vb" "".

.vb DeleteButtonField, . ( , Intellisense , 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>

<% @Page... >

<%@ Register TagPrefix="custom" Namespace="myControls" %> , "" GridView. web.config; .

Save the .aspx page and check. Now you have defined a common Sub (which defines the standard Delete button and its behavior), which you can connect to any GridView in your application.

0
source

Source: https://habr.com/ru/post/1767009/


All Articles