HI, I am looking a snippet to warn the user before processing with deletion and my ViewEmployees.aspx page.. I am using a Gridview with autogenerated insert, delete statements; how would I do this on my Code Behind File?

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
        DataKeyNames="EmployeeID" DataSourceID="AccessDataSource1" ForeColor="#333333" 
        GridLines="None">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
            <asp:BoundField DataField="EmployeeNumber" HeaderText="Employee Number" 
                SortExpression="EmployeeNumber" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Position" HeaderText="Position" 
                SortExpression="Position" />
            <asp:BoundField DataField="EmployeeSince" HeaderText="Employee Since" 
                SortExpression="EmployeeSince" />
            <asp:BoundField DataField="BusinessUnit" HeaderText="Business Unit" 
                SortExpression="BusinessUnit" />
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                ShowSelectButton="True" />
        </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>

Code Behind

Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting

        'Delete Warning 

        

    End Sub

Dani AI

Generated

asked for a simple JavaScript/VB confirm (no AJAX); pointed to the AJAX Confirm control, which is fine but not needed. The easiest, non-AJAX approach is to show a client-side confirm() on the Delete button so the postback only happens when the user clicks OK. Two practical options follow: attach the confirm in the code-behind (works with an autogenerated CommandField) or replace the CommandField with a TemplateField and set OnClientClick in markup.

Code-behind (no markup change) — hook RowDataBound and add OnClientClick to the Delete button you find in the row. This keeps your current CommandField and is robust if you don’t want to edit the GridView template:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        For i As Integer = 0 To e.Row.Cells.Count - 1
            For Each ctrl As Control In e.Row.Cells(i).Controls
                Dim lb As LinkButton = TryCast(ctrl, LinkButton)
                If lb IsNot Nothing AndAlso lb.CommandName = "Delete" Then
                    Dim empName As String = If(e.Row.DataItem IsNot Nothing, DataBinder.Eval(e.Row.DataItem, "Name").ToString(), "")
                    Dim safeName As String = empName.Replace("'", "\'")
                    lb.OnClientClick = "return confirm('Delete " & safeName & "?');"
                    Exit For
                End If
            Next
        Next
    End If
End Sub

TemplateField (markup) — replace your CommandField with a TemplateField and set OnClientClick directly:

<asp:TemplateField>
  <ItemTemplate>
    <asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" Text="Delete"
      OnClientClick="return confirm('Delete this employee?');" />
  </ItemTemplate>
</asp:TemplateField>

Notes and cautions: always escape single quotes in any injected text (see safeName above). Client-side confirm() can be bypassed if JavaScript is disabled, so keep server-side safety checks (in RowDeleting you can cancel with e.Cancel = True when needed). For details on the native API used here see Window.confirm on MDN.

HI, I am looking a snippet to warn the user before processing with deletion and my ViewEmployees.aspx page.. I am using a Gridview with autogenerated insert, delete statements; how would I do this on my Code Behind File?

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
        DataKeyNames="EmployeeID" DataSourceID="AccessDataSource1" ForeColor="#333333" 
        GridLines="None">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
            <asp:BoundField DataField="EmployeeNumber" HeaderText="Employee Number" 
                SortExpression="EmployeeNumber" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Position" HeaderText="Position" 
                SortExpression="Position" />
            <asp:BoundField DataField="EmployeeSince" HeaderText="Employee Since" 
                SortExpression="EmployeeSince" />
            <asp:BoundField DataField="BusinessUnit" HeaderText="Business Unit" 
                SortExpression="BusinessUnit" />
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                ShowSelectButton="True" />
        </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>

Code Behind

Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting

        'Delete Warning 

        

    End Sub

Hello,

check out this link that contains example of what you want.

You can download the sample source code from here. Also check out video provided there :)

I hope this helps!! :)

HI, I am looking a snippet to warn the user before processing with deletion and my ViewEmployees.aspx page.. I am using a Gridview with autogenerated insert, delete statements; how would I do this on my Code Behind File?

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
        DataKeyNames="EmployeeID" DataSourceID="AccessDataSource1" ForeColor="#333333" 
        GridLines="None">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
            <asp:BoundField DataField="EmployeeNumber" HeaderText="Employee Number" 
                SortExpression="EmployeeNumber" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Position" HeaderText="Position" 
                SortExpression="Position" />
            <asp:BoundField DataField="EmployeeSince" HeaderText="Employee Since" 
                SortExpression="EmployeeSince" />
            <asp:BoundField DataField="BusinessUnit" HeaderText="Business Unit" 
                SortExpression="BusinessUnit" />
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                ShowSelectButton="True" />
        </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>

Code Behind

Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting

        'Delete Warning 

        

    End Sub

Hello,

check out this link that contains example of what you want.

You can download the sample source code from here. Also check out video provided there :)

I hope this helps!! :)

Actually I am looking to do this in javascript or vb , dont want to mess to much in ajax. can you please explain what do I change on my code above ??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.