Skip to main content

How to create a confirm message with a Gridview when AutogenerateDelete="true"

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewDelete.aspx.cs" Inherits="GridViewDelete" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
      <div>
          <asp:GridView ID="grdTest" runat="Server" AutoGenerateColumns="true" AutoGenerateDeleteButton="true"
              OnRowDataBound="grdTest_RowDataBound">
          </asp:GridView>
      </div>
  </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class GridViewDelete : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {

       //Check For postback
       if (!IsPostBack)
       {
           BindGrid();
       }


   }
   protected void BindGrid()
   {
      

       List<Customer> customers = new List<Customer>();

       Customer cust1 = new Customer(1, "Cust 1");
       Customer cust2 = new Customer(2, "Cust 2");
       Customer cust3 = new Customer(3, "Cust 3");

       customers.Add(cust1);
       customers.Add(cust2);
       customers.Add(cust3);

       grdTest.DataSource = customers;
       grdTest.DataBind();

   }
   protected void grdTest_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       foreach (Control control in e.Row.Cells[0].Controls)
       {
           LinkButton DeleteButton = control as LinkButton;
           if (DeleteButton != null && DeleteButton.Text == "Delete")
           {
               DeleteButton.OnClientClick = "return(confirm('Are you sure you want to delete this record?'))";
           }
       }

   }
}


public class Customer
{
   public Customer(int id, string name)
   {
       ID = id;
       Name = name;
   }

   private int m_id;

   public int ID
   {
       get { return m_id; }
       set { m_id = value; }
   }

   private string m_name;

   public string Name
   {
       get { return m_name; }
       set { m_name = value; }
   }
}

Comments

  1. Who knows where to download XRumer 5.0 Palladium?
    Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

    ReplyDelete
  2. Oh, man...you saved me from pure insanity. I was looking for this solution for hours. THANK YOU!!!

    ReplyDelete
  3. Thank you! This is exactly what I was looking for.

    ReplyDelete
  4. Thanks for you code!

    ReplyDelete
  5. Hi ,
    Thanks for this good post.

    regards,
    bhaskar
    http://csharpektroncmssql.blogspot.com

    ReplyDelete

Post a Comment

Popular posts from this blog