Delete Confirmation In a GridView

Self-Innovator
Posted by Self-Innovator under ASP.NET category on | Points: 40 | Views : 1316
Page Design .aspx

 <asp:GridView ID="GridView1" runat="server" DataKeyNames="id" AutoGenerateColumns="False" AllowPaging="True" 
onpageindexchanging="GridView1_PageIndexChanging" CellPadding="6" CellSpacing="3" PageSize="50"
onrowdeleting="GridView1_RowDeleting" onrowdatabound="GridView1_RowDataBound"
onselectedindexchanged="GridView1_SelectedIndexChanged" HeaderStyle-BackColor="#FFAA2B" HeaderStyle-ForeColor="White" RowStyle-BackColor="#FFE8C4" AlternatingRowStyle-BackColor="#FFD393">
<Columns>
<asp:CommandField ButtonType="Link" HeaderText="Delete"
ShowDeleteButton="True" />

</Columns>
</asp:GridView>


Code Behind
Row DataBound Event
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow) return;
int lastCellIndex = e.Row.Cells.Count - 1;
LinkButton deleteButton = (LinkButton)e.Row.Cells[lastCellIndex].Controls[0];
deleteButton.OnClientClick =
"if (!window.confirm('Are you sure you want to delete this assignment?')) return false;";
}

On Row Deleting Event
 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
myClass m=new myClass();
MessageBox msg = new MessageBox();
m.deleteassignment(id);
fillgrid();
msg.Show("Deleted successfully");
}

Comments or Responses

Login to post response