Using System Using System - Data Using System - Configuration Using System - Collections
Using System Using System - Data Using System - Configuration Using System - Collections
What is the logic behind the Edit, Update, Cancel and Delete?
1) Add link buttons Edit with command name “Edit”, Delete with Command Name
”Delete” in Item template.
2) Also set the Edit Item Template of the datalist to show the data in a textbox
(See the html view of the edit item template), also add the link buttons Update, Cancel with corresponding
command name “Update and Cancel”.
3) Set the events edit command, update command, delete command and cancel
Command of datalist similar in the grid view.
4) Also put the code given in the sample cs code.
Note: By setting the datakeyfield of the datalist, we can easily identify which row is going to update or delete.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;
using System.Data.SqlClient;
#endregion
#region Events
if (!IsPostBack)
bindDataList();
}
protected void DataList1_CancelCommand(object source,
DataListCommandEventArgs e)
DataList1.EditItemIndex = -1;
bindDataList();
DataListCommandEventArgs e)
_con.Open();
+ DataList1.DataKeys[e.Item.ItemIndex], _con);
_cmd.ExecuteNonQuery();
_con.Close();
DataList1.EditItemIndex = -1;
bindDataList();
DataListCommandEventArgs e)
_con.Open();
SqlCommand _cmd = new SqlCommand();
if (e.Item.ItemIndex != 0)
DataList1.DataKeys[e.Item.ItemIndex], _con);
else
_cmd.ExecuteNonQuery();
_con.Close();
DataList1.EditItemIndex = -1;
bindDataList();
DataListCommandEventArgs e)
DataList1.EditItemIndex = e.Item.ItemIndex;
bindDataList();
#endregion
#region Private Function
///
///
_con.Open();
_da.Fill(_ds);
_con.Close();
DataRow dr = _ds.Tables[0].NewRow();
_ds.Tables[0].Rows.InsertAt(dr, 0);
DataList1.DataSource = _ds;
DataList1.DataBind();
((LinkButton)DataList1.Items[0].FindControl("lnkEdit")).Text = "Insert";
((LinkButton)DataList1.Items[0].FindControl("lnkDelete")).Visible = false;
///
///
///
#endregion