Live Chat Icon For mobile
Live Chat Icon

How to confirm delete in DataGrid using LinkButton

Platform: ASP.NET| Category: DataGrid

<script>
function  confirmmsg() 
{ 
	if (confirm('Do you want to delete record?')==true) 
		return true; 
	else 
		return false; 
}
</script>

<asp:DataGrid id='DataGrid1' AutoGenerateColumns='False' DataKeyField='Employeeid' OnItemCommand='ItemCmd' OnItemDataBound='ItemDB'  runat='server'>
<Columns>
	<asp:BoundColumn DataField='firstname' HeaderText='First Name'></asp:BoundColumn>
	<asp:TemplateColumn>
		<ItemTemplate>
			<asp:LinkButton id='btnDelete' runat='server' Text='Delete' CommandName='Delete' CausesValidation='false'></asp:LinkButton>
		</ItemTemplate>
	</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

VB.NET


Dim sqlStmt As String
Dim conString As String
Dim cn As SqlConnection = Nothing
Dim da As SqlDataAdapter = Nothing
Dim ds As DataSet

Private Sub Page_Load(sender As Object, e As System.EventArgs)
   	conString = 'server=localhost;database=Northwind;uid=sa;pwd=;'
   	cn = New SqlConnection(conString)
   	If Not Page.IsPostBack Then
     		BindData()
   	End If
End Sub ’Page_Load

Sub BindData()
   	sqlStmt = 'select * from emp  '
   	ds = New DataSet()
   	da = New SqlDataAdapter(sqlStmt, cn)
   	da.Fill(ds, 't1')
   	DataGrid1.DataSource = ds
   	DataGrid1.DataBind()
End Sub ’BindData

Protected Sub ItemDB(sender As Object, e As System.Web.UI.WebControls.DataGridItemEventArgs)
   	Select Case e.Item.ItemType
      		Case ListItemType.Item, ListItemType.AlternatingItem
            			Dim btn As LinkButton = CType(e.Item.FindControl('btnDelete'), LinkButton)
            			btn.Attributes.Add('onclick', 'return confirmmsg();')
            			Exit 
   	End Select
End Sub ’ItemDB

Protected Sub ItemCmd(sender As Object, e As System.Web.UI.WebControls.DataGridCommandEventArgs)
   	If e.CommandName = 'Delete' Then
      		Me.DeleteRow(Me.DataGrid1.DataKeys(e.Item.ItemIndex).ToString())
   	End If
   	BindData()
End Sub ’ItemCmd

Private Sub DeleteRow(empid As String)
   	Dim cmd As New SqlCommand('DELETE FROM Emp WHERE employeeid =' + empid, cn)
   	cn.Open()
   	cmd.ExecuteNonQuery()
   	cn.Close()
End Sub ’DeleteRow

C#


string  sqlStmt ; 
string  conString ; 
SqlConnection cn =null; 
SqlDataAdapter da =null; 
DataSet ds;
private void Page_Load(object sender, System.EventArgs e)
{
	conString = 'server=localhost;database=Northwind;uid=sa;pwd=;';
	cn = new SqlConnection(conString);
	if (!Page.IsPostBack )
	{
		BindData();
	}
}
void BindData()
{
	sqlStmt = 'select * from emp  ';
	ds= new DataSet ();
	da = new SqlDataAdapter (sqlStmt, cn);
	da.Fill (ds,'t1');
	DataGrid1.DataSource =ds;
	DataGrid1.DataBind ();
}

protected void ItemDB(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
	switch(e.Item.ItemType)
	{ 
		case ListItemType.Item: 
		case ListItemType.AlternatingItem: 
		{ 
			LinkButton  btn = (LinkButton)e.Item.FindControl('btnDelete'); 
			btn.Attributes.Add('onclick', 'return confirmmsg();'); 
			break; 
		}
	}
}

protected void ItemCmd(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e) 
{ 
	if(e.CommandName == 'Delete')
	{ 
		this.DeleteRow(this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString()); 
	} 
	BindData();
} 

private void DeleteRow(string empid)
{ 
	SqlCommand cmd = new SqlCommand('DELETE FROM Emp WHERE employeeid ='+ empid ,cn); 
	cn.Open(); 
	cmd.ExecuteNonQuery(); 
	cn.Close(); 
} 


Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.