Introduction
Here I will explain how to delete records in gridview with confirmationbox message in asp.net.
Description:
I designed gridview with linkbutton that contains user information details. Here my requirement is whenever user clicks on link button at that time I need to display confirmation message and if user clicks on ok button in confirmation box I want to delete record from database otherwise no action should perform on particular record. For that we need to follow below steps
Design your aspx page like this
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ConfirmBox in Gridview Row Delete</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
}
</style>
<script type="text/javascript">
function ConfirmationBox(username) {
var result = confirm('Are you sure you want to delete '+username+' Details' );
if (result) {
return true;
}
else {
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gvrecords" CssClass="Gridview"
DataKeyNames="UserId" AutoGenerateColumns="false"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White"
onrowdatabound="gvrecords_RowDataBound">
<Columns>
<asp:BoundField DataField="UserName" HeaderText="UserName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:TemplateField HeaderText="Delete User Details">
<ItemTemplate>
<asp:LinkButton ID="lnkdelete" runat="server" OnClick="lnkdelete_Click">Delete User</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
|
After that write the following code in code behind
SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUserDetails();
}
}
protected void BindUserDetails()
{
//connection open
con.Open();
//sql command to execute query from database
SqlCommand cmd = new SqlCommand("Select * from UserDetails",con);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
//Binding data to gridview
gvrecords.DataSource = ds;
gvrecords.DataBind();
con.Close();
}
protected void gvrecords_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//getting username from particular row
string username =Convert.ToString(DataBinder.Eval(e.Row.DataItem,"UserName"));
//identifying the control in gridview
LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkdelete");
//raising javascript confirmationbox whenver user clicks on link button
lnkbtnresult.Attributes.Add("onclick", "javascript:return ConfirmationBox('" + username+ "')");
}
}
protected void lnkdelete_Click(object sender, EventArgs e)
{
LinkButton lnkbtn= sender as LinkButton;
//getting particular row linkbutton
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
//getting userid of particular row
int userid = Convert.ToInt32(gvrecords.DataKeys[gvrow.RowIndex].Value.ToString());
string username = gvrow.Cells[0].Text;
con.Open();
SqlCommand cmd = new SqlCommand("delete from UserDetails where UserId="+userid,con);
int result=cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindUserDetails();
//Displaying alert message after successfully deletion of user
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + username + " details deleted successfully')", true);
}
}
|
Demo
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
14 comments :
Really Goodm, Easily understandable for Beginners
This code very useful for me.
thanks for your help.
easily understand.
by mohankumar.
Thanks it is really helpful
i cant use datetime picker in master page
shagulgulam@gmail.com
Thanks it is really helpful
Thank you, this article is really very helpful.
Thaks this code is work for me
I've spent half a day trying to find how to do this exact thing, and it is perfect. Easy to understand even for novices such as myself. Thank you for taking the time to put this together and share with others!!
Thanks for your wonderful article. Its really saved my time and help me lot!.
thanks this gr8 article .its really helpful article
In Gridview I Have Selected Auto Generate Delete Button ......So How To Apply This Code In That........
Very good . . .sureshbhai
how to use link button in gridview. Is it possible?.. Plz any one explain..
Thynks...its really very helpfull..
Note: Only a member of this blog may post a comment.