Here, we will learn how to edit, update, delete record to DetailsView in ASP.Net C#.
How to Display record in DetailsView in ASP.Net ?
How to do paging in DetailsView control in ASP.Net ?
First Bind Data to DetailsView control from sql server..
For Editing set AutogenerateEditButton = True at property of DetailsView control.
Now, we have Edit button on DetailsView control in ASP.Net.
When we click Edit Button that time we need to change mode of DetailsView control for Editing Records.
The CurrentMode of DetailsView contorl is ReadOnly we change the mode to Edit for Editing Record..
write below code at ModeChanging Event of DetailsView control for go to Edit mode.
We handle Edit Click Event and Cancel Click Event here…
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.ReadOnly)
{
DetailsView1.ChangeMode(DetailsViewMode.Edit);
BindDetailsView();
}
else if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
BindDetailsView();
}
}
Now click Edit button to enable Edit mode of Details View control….
If you click Cancel Button then DetailsView back to original Readonly mode.
Here, you can change value of Name and City click update button to update Record to database.
For update we take here ItemUpdating Event…
write below code at ItemUpdating Event of DetailsView Control in ASP.Net.
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
int idd =Convert.ToInt32(DetailsView1.Rows[0].Cells[1].Text);
TextBox name = DetailsView1.Rows[1].Cells[1].Controls[0] as TextBox;
TextBox city = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox;SqlDataAdapter SQLAdapter = new SqlDataAdapter(“update UserMst set name='” + name.Text + “‘, city='” + city.Text + “‘ where id=”+idd+””, SQLConn);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
BindDetailsView();
}
The updated result is :
Now, we will learn to Delete Record from DetailsView in ASP.Net..
Here, set AutogenerateDeleteButton = True of DetailsView control.
write code at ItemDeleting Event of DetialsView control for Delete record from DetialsView.
write below code at ItemDeleting event for Delete Record form Database….
protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
int idd = Convert.ToInt32(DetailsView1.DataKey[e.RowIndex].ToString());SqlDataAdapter SQLAdapter = new SqlDataAdapter(“delete from UserMst where id=” + idd + “”, SQLConn);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);BindDetailsView();
}
I hope you enjoy with lean this ASP.Net Edit, update, delete Detialsview example…..