Delete Confirmation in GridView

Posted by Self-innovator under ASP.NET on 1/15/2012 | Points: 10 | Views : 6489 | Status : [Member] | Replies : 7
Hi friends..
Just tell me i have an gridview with some data when i click on delete link it asks with msg box ('are you sure want to delete this user') here i wanted to show the msg box like('are you sure want to delete this user') with selected user name how do i bind the user name. I've tried with databinder .eval but not working...
This is my code:OnClientClick="return confirm('Do you want to delete details of this person')"

Join Hands Change lives
Thanks & Regards
Straight Edge Society



Responses

Posted by: Sksamantaray on: 1/16/2012 [Member] Silver | Points: 25

Up
0
Down
           <asp:TemplateField HeaderText="Action">

<ItemTemplate>
<span onclick="return confirm('Are you sure to Edit?')">
<asp:LinkButton ID="LnkbtnEdit" runat="server" Text="Edit" OnClick="LnkbtnEdit_Click" style="text-decoration:none"></asp:LinkButton>
</span>
</ItemTemplate>

<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />

</asp:TemplateField>


Try This..

Thanks,
Sanjay

Self-innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-innovator on: 1/16/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi sksamantaray...
Pls go through my Code when i click on delete link in Gridview it just asks me ('Are you sure do you want to delete this person') instead i wanted to popup msg box along with the selected username Ex:-('Are you sure to delete the records of Person "ANIL')

DesignPage
<asp:TemplateField HeaderText="Delete">

<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server" OnClientClick="return confirm('Do you want to delete details of this person')" CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>


Code .cs

protected void gvPerson_RowDeleting(object sender, GridViewDeleteEventArgs e)

{
BAL = new PersonBusinesslayer();
int id = Convert.ToInt32(((Label)gvPerson.Rows[e.RowIndex].FindControl("lblId")).Text);
try
{
BAL.Delete(id);
ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('Record deleted successfully');</script>");
gvPerson.EditIndex = -1;
BindTopId();
BindGrid();
}
catch { }
}


Join Hands Change lives
Thanks & Regards
Straight Edge Society

Self-innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 1/16/2012 [Member] Silver | Points: 25

Up
0
Down


use modal pupup extender

Thanks,
Sanjay

Self-innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chandu_504 on: 3/7/2013 [Member] Starter | Points: 25

Up
0
Down
look at the sample code
http://code.msdn.microsoft.com/How-to-add-delete-dc1bf730

Chandrasekhar

Self-innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 3/7/2013 [Member] [MVP] Gold | Points: 25

Up
0
Down
Try this,
 

<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="LblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

Write this code on Gird View Row-Data-Bound

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string strName = string.Empty;
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnk = (LinkButton)e.Row.FindControl("lnkDelete");
Label LblName = (Label)e.Row.FindControl("LblName");
strName = LblName.Text;
lnk.OnClientClick = "javascript:return confirm('Do you want to delete details of this person " + strName + "?');";

}
}


Please Mark As Answer if this helps you

.

Self-innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 3/7/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 DataKeyNames="sid" ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting">
<Columns>

<asp:BoundField DataField="sid" HeaderText="sid" />

<asp:BoundField DataField="sname" HeaderText="sname" />

<asp:TemplateField HeaderText="Select">

<ItemTemplate>

<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("sid") %>' CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>
</div>
</form>
</body>
</html>

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand();
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}

private void BindGrid()
{
con = new SqlConnection(connStr);
cmd.Connection = con;
cmd.CommandText = "Select * from student";
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
l.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete this record:: " + DataBinder.Eval(e.Row.DataItem, "sid") + "')");
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sid = (string)GridView1.DataKeys[e.RowIndex].Value;
SqlConnection con = new SqlConnection(connStr);
cmd.Connection = con;
cmd.CommandText = "DELETE FROM student WHERE sid='" + sid + "'";
con.Open();
cmd.ExecuteNonQuery();
con.Close();
BindGrid();
}
}


If this post helps you mark it as answer
Thanks

Self-innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 3/8/2013 [Member] Starter | Points: 25

Up
0
Down
OnClientClick="return confirm('Are you sure u want to delete this entry');"

This works

Self-innovator, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response