Tuesday 1 April 2014

How to delete records in gridview with JavaScript confirmation message box in ASP.NET

In this article I will explain how to delete records in gridview with JavaScript 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 JavaScript Code:

<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<span onclick="return confirm(‘are you sure you Want to delete this  record?')">
<asp:LinkButton ID="lnkDelete" runat="server" OnClick="lnkDelete_Click" Text="Delete"></asp:LinkButton>
</span>
</ItemTemplate>
</asp:TemplateField>


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-familyArial;
font-size10pt;
font-weightnormal;
colorwhite;
}
</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">
<Columns>
<asp:BoundField DataField="FileID" HeaderText="Id" ItemStyle-ForeColor="Black" />
<asp:BoundField DataField="FileName" HeaderText="FileName" ItemStyle-ForeColor="Black" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<span onclick="return confirm('are you sure you Want to delete this  record?')">
<asp:LinkButton ID="lnkDelete" runat="server" OnClick="lnkDelete_Click" Text="Delete"></asp:LinkButton>
</span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

Design :





Below is the total code for GridView Row data deleting.

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();
}
}
}

Output:

No comments:

Post a Comment