Saturday, 29 March 2014

Delete gridview records with confirmation message box example in ASP.NET

In this article I will explain how to delete records in gridview with confirmation message box in ASP.NET.


User clicks on Delete link button in GridView at that time I need to show confirmation message box and if user clicks on ok button in confirmation message box I want to delete record from database and rebind the gridview otherwise no action should perform on particular record.

Below is the Sample Code:

protected void gvFileDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btnDelete = (LinkButton)e.Row.FindControl("lnkDelete");
if (btnDelete != null)
{
btnDelete.Attributes.Add("onclick", "javascript:return " +
"confirm('Are you sure you want to delete this record?')");
}
}
}


Total code:

First we need to establish a connection in Web.config file.

<connectionStrings>
<add name="connectionString" connectionString="Data Source=LocalHost;uid=sa1;password=Con@123;Initial Catalog=practice"/>
</connectionStrings>
Below is the page design.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.gvColours {
font-family: Arial;
font-size: 10pt;
font-weight: normal;
color: white;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gvFileDetails" AutoGenerateColumns="false" DataKeyNames="FileID"
CssClass="gvColours" Width="500px" HeaderStyle-BackColor="Maroon" OnRowDataBound="gvFileDetails_RowDataBound">
<Columns>
<asp:BoundField DataField="FileID" HeaderText="Id" ItemStyle-ForeColor="Black" />
<asp:BoundField DataField="FileName" HeaderText="FileName" ItemStyle-ForeColor="Black" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server" OnClick="lnkDelete_Click" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

Design :



Below is the total code for GridView conformation message box .

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web.UI.WebControls;

public partial class DeleteRecord : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindFileInfoToGrid();
}
}
// Bind Data to Gridview use DataSet
private void BindFileInfoToGrid()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from FileDetails", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet dsFileInfo = new DataSet();
da.Fill(dsFileInfo);
con.Close();
gvFileDetails.DataSource = dsFileInfo;
gvFileDetails.DataBind();
}

// This Link Button click event is used to Delete files from gridview
protected void lnkDelete_Click(object sender, EventArgs e)
{
LinkButton lnkDeleteLinkButton = sender as LinkButton;
GridViewRow gvFindRow = lnkDeleteLinkButton.NamingContainer as GridViewRow;
int FileID = Convert.ToInt32(gvFileDetails.DataKeys[gvFindRow.RowIndex].Value.ToString());
con.Open();
SqlCommand cmd = new SqlCommand("delete from FileDetails where FileID=" + FileID, con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindFileInfoToGrid();
}
}
protected void gvFileDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btnDelete = (LinkButton)e.Row.FindControl("lnkDelete");
if (btnDelete != null)
{
btnDelete.Attributes.Add("onclick", "javascript:return " +
"confirm('Are you sure you want to delete this record?')");
}
}
}
}

Output:




1 comment: