Live Chat Icon For mobile
Live Chat Icon

How to confirm delete in DataGrid using PushButton

Platform: ASP.NET| Category: DataGrid

<asp:DataGrid id='DataGrid1' AutoGenerateColumns='False' OnDeleteCommand ='DelCmd' OnItemCreated ='ItemCrt' DataKeyField='Employeeid'   runat='server'>
<Columns>
	<asp:ButtonColumn Text='Delete' ButtonType='PushButton' CommandName='Delete'></asp:ButtonColumn>
	<asp:BoundColumn DataField='firstname' HeaderText='First Name'></asp:BoundColumn>
</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 ItemCrt(sender As Object, e As DataGridItemEventArgs)
   	Select Case e.Item.ItemType
      		Case ListItemType.Item, ListItemType.AlternatingItem
            		Dim btn As Button = CType(e.Item.Cells(0).Controls(0), Button)
            		btn.Attributes.Add('onclick', 'return confirm(’are you sure you want to delete this’)')
            		Exit 
   	End Select
End Sub ’ItemCrt


Protected Sub DelCmd(sender As [Object], e As DataGridCommandEventArgs)
   	DeleteRow(Me.DataGrid1.DataKeys(e.Item.ItemIndex).ToString())
   	BindData()
End Sub ’DelCmd


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 ItemCrt(object sender, DataGridItemEventArgs e) 
{
	switch(e.Item.ItemType)
	{ 
		case ListItemType.Item: 
		case ListItemType.AlternatingItem: 
		{
			Button btn = (Button)e.Item.Cells[0].Controls[0]; 
			btn.Attributes.Add('onclick',
			'return confirm(’are you sure you want to delete this’)'); 
			break;
		} 
	}	 
}

protected void  DelCmd(Object sender   , DataGridCommandEventArgs e   ) 
{
	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

Share on twitter
Share on facebook
Share on linkedin