It is good practice and user
friendly to confirm before deleting record from gridview. In this article we
will learn how to confirm from user before actually deleting the record in gridview
.
Add a gridview on webpage and
add columns which we want to show on the webpage using boundfields of gridview.
Now add Template field and add one link button inside template field. “OnClientClick
“ of LinkButton call javascript function i.e. “Confirm” to confirm before
deleting record as given below:
<asp:TemplateField HeaderText="Delete"
ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete"
runat="server"
CausesValidation="False"
CommandName="Delete" " OnClientClick="javascript: return confirm('Do you want to
delete this record?')" CommandArgument='<%#bind("Student_id") %>' Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
|
When user presses ok, record
will be deleted. Code written in code behind file will be executed. If presses
NO control will not go to code behind file.
Implementation:
ASP.Net Page design: Add gridview control on the webpage and customize it as given
below:
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
BackColor="White"
BorderColor="#CCCCCC"
BorderStyle="None"
BorderWidth="1px"
CellPadding="3"
Width="270px"
onrowcommand="GridView1_RowCommand"
onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="Student_name" HeaderText="Student Name" />
<asp:BoundField DataField="Age"
HeaderText="Age"
/>
<asp:BoundField DataField="Class"
HeaderText="Class"
/>
<asp:TemplateField HeaderText="Delete"
ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete"
runat="server"
CausesValidation="False"
CommandName="Delete"
OnClientClick="javascript:
return confirm('Do you want to delete this record?')" CommandArgument='<%#bind("Student_id") %>' Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle
BackColor="White"
ForeColor="#000066"
/>
<HeaderStyle
BackColor="#006699"
Font-Bold="True"
ForeColor="White"
/>
<PagerStyle
BackColor="White"
ForeColor="#000066"
HorizontalAlign="Left"
/>
<RowStyle
ForeColor="#000066"
/>
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle
BackColor="#F1F1F1"
/>
<SortedAscendingHeaderStyle
BackColor="#007DBB"
/>
<SortedDescendingCellStyle
BackColor="#CAC9C9"
/>
<SortedDescendingHeaderStyle
BackColor="#00547E"
/>
</asp:GridView>
|
ASP.NET code behind File using C#:
Write following code on RowCommand event to delete record.
protected void GridView1_RowCommand(object
sender, GridViewCommandEventArgs e)
{
int
studentid =Convert.ToInt32(
e.CommandArgument.ToString());
if
(e.CommandName =="Delete")
{
if
(con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
SqlCommand
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "Delete from student_Info where student_id="
+ studentid + "";
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
Bind_grid();
}
}
protected
void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs
e)
{
}
|
VB.NET code behind file:
Now Write Following
Code on gridview_Rowcommand event to delete Record:
Protected Sub
GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
Dim
studentid As Integer
= Convert.ToInt32(e.CommandArgument.ToString())
If
e.CommandName = "Delete" Then
If
con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
Dim
cmd As New
SqlCommand()
cmd.Connection = con
cmd.CommandText = "Delete from student_Info where student_id="
+ studentid + ""
cmd.ExecuteNonQuery()
cmd.Dispose()
con.Close()
Bind_grid()
End If
End Sub
Protected Sub
GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
End Sub
|
Demo:
Use this code, create your own
application and check result. You can use this code in any application by
customizing it. If you have any doubt, you can ask by commenting in comment
section.