Hi fkz2899,
Refer below sample.
HTML
<asp:GridView ID="GridView1" CssClass="Grid" runat="server" OnRowDeleting="GridView1_OnRowDeleting"
AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Item" HeaderText="Item" />
<asp:BoundField DataField="Price" HeaderText="Price" />
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
</Columns>
</asp:GridView>
<div style="text-align: center; width: 250px; margin-bottom: 10px">
<b>
<asp:Button ID="dd" class="btn btn-warning" Style="text-align: center; margin-left: 100%"
runat="server" OnClick="dd_Click" OnClientClick="return confirm('Are you sure you want to Place Order?');"
Text="Place Order" /></b></div>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Item"), new DataColumn("Price") });
dt.Rows.Add("Shirt", 450);
dt.Rows.Add("Jeans", 3200);
dt.Rows.Add("Trousers", 1900);
Session["buy"] = dt;
BindGrid();
}
}
protected void BindGrid()
{
GridView1.DataSource = Session["buy"] as DataTable;
GridView1.DataBind();
}
protected void GridView1_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dt = new DataTable();
dt = (DataTable)Session["buy"];
dt.Rows[e.RowIndex].Delete();
dt.AcceptChanges();
if (dt.Rows.Count == 0)
{
dd.OnClientClick = null;
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string item = e.Row.Cells[0].Text;
foreach (Button button in e.Row.Cells[2].Controls.OfType<Button>())
{
if (button.CommandName == "Delete")
{
button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + item + "?')){ return false; };";
}
}
}
}
protected void dd_Click(object sender, EventArgs e)
{
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Item"), New DataColumn("Price")})
dt.Rows.Add("Shirt", 450)
dt.Rows.Add("Jeans", 3200)
dt.Rows.Add("Trousers", 1900)
Session("buy") = dt
BindGrid()
End If
End Sub
Protected Sub BindGrid()
GridView1.DataSource = TryCast(Session("buy"), DataTable)
GridView1.DataBind()
End Sub
Protected Sub GridView1_OnRowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim dt As DataTable = New DataTable()
dt = CType(Session("buy"), DataTable)
dt.Rows(e.RowIndex).Delete()
dt.AcceptChanges()
If dt.Rows.Count = 0 Then
dd.OnClientClick = Nothing
End If
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim item As String = e.Row.Cells(0).Text
For Each button As Button In e.Row.Cells(2).Controls.OfType(Of Button)()
If button.CommandName = "Delete" Then
button.Attributes("onclick") = "if(!confirm('Do you want to delete " & item & "?')){ return false; };"
End If
Next
End If
End Sub
Protected Sub dd_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub