here i am posting code for custom datalist
in which updation ,deletion and editng is implemented
hope this help,
code for .aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Datalist.aspx.cs" Inherits="Datalist" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" align="center" Width="80%"
oncancelcommand="DataList1_CancelCommand"
ondeletecommand="DataList1_DeleteCommand" oneditcommand="DataList1_EditCommand"
onupdatecommand="DataList1_UpdateCommand">
<HeaderTemplate>
<table class="headertable" cellpadding="5" cellspacing="5">
<tr>
<th class="headertd">
Id
</th>
<th class="headertd">
Name
</th>
<th class="headertd">
Class
</th>
<th class="headertd">
Edit
</th>
<th class="headertd">
Delete
</th>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table class="headertable" cellpadding="5" cellspacing="7">
<tr>
<td class="itemtd">
<asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>'></asp:Label>
</td>
<td class="itemtd">
<asp:Label ID="Label2" runat="server" Text='<%#Eval("name") %>'></asp:Label>
</td>
<td class="itemtd">
<asp:Label ID="Label3" runat="server" Text='<%#Eval("class") %>'></asp:Label>
</td>
<td class="itemtd">
<asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Edit" />
</td>
<td class="itemtd">
<asp:Button ID="Button2" runat="server" Text="Delete" CommandName="Delete" />
</td>
</tr>
</table>
</ItemTemplate>
<AlternatingItemTemplate>
<table class="headertable" cellpadding="5" cellspacing="5">
<tr>
<td class="itemtd">
<asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>'></asp:Label>
</td>
<td class="itemtd">
<asp:Label ID="Label2" runat="server" Text='<%#Eval("name") %>'></asp:Label>
</td>
<td class="itemtd">
<asp:Label ID="Label3" runat="server" Text='<%#Eval("class") %>'></asp:Label>
</td>
<td class="itemtd">
<asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Edit" />
</td>
<td class="itemtd">
<asp:Button ID="Button2" runat="server" Text="Delete" CommandName="Delete" />
</td>
</tr>
</table>
</AlternatingItemTemplate>
<EditItemTemplate>
<table class="headertable" cellpadding="5" cellspacing="5">
<tr>
<td class="itemtd">
<asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>'></asp:Label>
</td>
<td class="itemtd">
<asp:TextBox ID="txt1" runat="server" Text='<%#Eval("name") %>'></asp:TextBox>
</td>
<td class="itemtd">
<asp:TextBox ID="txt2" runat="server" Text='<%#Eval("class") %>'></asp:TextBox>
</td>
<td class="itemtd">
<asp:Button ID="Button1" runat="server" Text="Update" CommandName="Update" />
<asp:Button ID="Button3" runat="server" Text="Cancel" CommandName="Cancel" />
</td>
<td class="itemtd">
<asp:Button ID="Button2" runat="server" Text="Delete" CommandName="Delete" />
</td>
</tr>
</table>
</EditItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
and here is the code for .aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;
public partial class Datalist : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstring"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
}
public void getdatasp()
{
con.Open();
SqlCommand cmd = new SqlCommand("showdata", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader();
DataList1.DataSource = dr;
DataList1.DataBind();
dr.Close();
con.Close();
}
protected void Button4_Click(object sender, EventArgs e)
{
getdatasp();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
getdatasp();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
getdatasp();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
int _id = Convert.ToInt32(((Label)e.Item.FindControl("Label1")).Text);
string _name = ((TextBox)e.Item.FindControl("txt1")).Text;
string _class = ((TextBox)e.Item.FindControl("txt2")).Text;
con.Open();
SqlCommand cmd = new SqlCommand("updatedata", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = _id;
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = _name;
cmd.Parameters.Add("@class", SqlDbType.VarChar).Value = _class;
cmd.ExecuteNonQuery();
con.Close();
DataList1.EditItemIndex = -1;
getdatasp();
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
int _id = Convert.ToInt32(((Label)DataList1.FindControl("Label1")).Text);
con.Open();
SqlCommand cmd = new SqlCommand("deletedata", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = _id;
cmd.ExecuteNonQuery();
con.Close();
DataList1.EditItemIndex = -1;
getdatasp();
}
}