ASP.NET (35) SQL (25) JAVASCRIPT (24) HTML (14) STYLE SHEET (6) ASP (4) SCRIPT (1)

Search me out HERE!!

Confirm delete for GridView's CommandField using JavaScript

This CODE will help you to implement Confirm (Using Javascript) When click on DELETE in GRIDVIEW

For this you need to use asp:linkbutton

CODE For Gridview in test.aspx file is

<asp:gridview id="gv_test" onrowdatabound="gv_test_OnRowDataBound" runat="server" >

< columns >
< asp:templatefield headertext="Delete" >
< itemtemplate >

< asp:linkbutton commandargument='<%# Eval("id") %>' commandname="cmdDelete" id="btnDelete" onclientclick="javascript: return confirm('do u want to delete this record')" runat="server" >  DELETE  </ asp:linkbutton >

</ itemtemplate >
</ asp:templatefield >
</ columns >


CODE for  gv_test_OnRowDataBound Method in test.aspx.cs File is

protected void GridView1_RowCommand(object sender,  GridViewCommandEventArgs e)
    { 

//Through command  Name link Button will be identified.
        if (e.CommandName == "cmdDelete")
        {
            // This is id which you can use for DELETE purpose.
            string id = e.CommandArgument;
                       
        }
    }


Using above code you can get confirm before Delete in GRIDVIEW as given below:



No comments:

Post a Comment