Details View Updating using custom Edit and Update buttons
Hi
While working with DetailsView control I have come up with few resources when you want to perform update to the data within DetailsView using buttons out side of DetailsView control.
Here is the source code to update all the columns in DetailsView with edited data. Note that regard less of old values and new values database is updated with all the data in DetailsView(edited columns and non-edited columns).
//ASPX
<form id="form1" runat="server">
<div>
<asp:Button ID="btnEdit" runat="server" Text="Edit" onclick="btnEdit_Click" />
<asp:Button ID="btnUpdate" runat="server" Text="Update" onclick="btnUpdate_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" onclick="btnCancel_Click" />
<br /><br />
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"
AutoGenerateRows="false" DataKeyNames="CustomerId"
onitemupdating="DetailsView1_ItemUpdating">
<Fields>
<asp:BoundField HeaderText="ID" DataField="CustomerId" />
<asp:BoundField HeaderText="Name" DataField="CustomerName" />
<asp:BoundField HeaderText="Deaprtment" DataField="Department" />
<asp:BoundField HeaderText="Address" DataField="address" />
<asp:BoundField HeaderText="Married" DataField="Married" />
<asp:BoundField HeaderText="Nationality" DataField="Nationality" />
</Fields>
</asp:DetailsView>
</div>
</form>
//code behind c#
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class View : System.Web.UI.Page
{
public string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
//Check if the page is the result of PostBack
if((!IsPostBack)
{
//Bind data
DetailsView1.DataSource = GetUser();
DetailsView1.DataBind();
}
}
/// <summary>
/// Get data
/// </summary>
/// <returns>DataTable with Customers data</returns>
private DataTable GetUser()
{
string custId = Request.QueryString["id"].ToString();
SqlConnection con = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("Select * From customer Where CustomerId= '" + custId + "' ", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable CustomerTable = new DataTable();
adapter.Fill(CustomerTable);
return CustomerTable;
}
/// <summary>
/// Edit button click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnEdit_Click(object sender, EventArgs e)
{
DetailsView1.ChangeMode(DetailsViewMode.Edit);
//Bind the DetailsView with data
DetailsView1.DataSource = GetUser();
DetailsView1.Databind();
}
/// <summary>
/// Cancel button click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnCancel_Click(object sender, EventArgs e)
{
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
//Change 3: Bind the DetailsView with custom method (i.e., GetUser())
DetailsView1.DataSource = GetUser();
DetailsView1.Databind();
}
/// <summary>
/// Update button click event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnUpdate_Click(object sender, EventArgs e)
{
//http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.updateitem.aspx
DetailsView1.UpdateItem(true);
}
/// <summary>
/// DetailsView updating event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
if(DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
//Get DetailsView columns
TextBox txtUserName = DetailsView1.Rows[3].Cells[1].Controls[0] as TextBox;
//Note that you need FindControl for TemplateFields in DetailsView
TextBox txtUserID = DetailsView1.FindControl("txtUserID");
SqlConnection con = new SqlConnection((conString));
using (SqlCommand cmd = new SqlCommand("spUpdateUserRecord", Con))
{
//Specify the command type
cmd.CommandType = CommandType.StoredProcedure;
//Pass parameters
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@UserID", txtUserID.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
}
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
//Change 4: Bind the DetailsView with custom method (i.e., GetUser())
DetailsView1.DataSource = GetUser();
DetailsView1.Databind();
}//end of if
}//end of Event
}
References