Hi micah,
Check this example. Now please take its reference and correct your code.
HTML
<asp:DataList ID="dlCustomers" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Country</th>
<th></th>
<th></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="lblId" Text='<%#Eval("Id") %>' runat="server" /></td>
<td><asp:Label ID="lblName" Text='<%#Eval("Name") %>' runat="server" /></td>
<td><asp:Label ID="lblCountry" Text='<%#Eval("Country") %>' runat="server" /></td>
<td><asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" OnClick="OnEdit" /></td>
<td>
<asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" OnClick="OnDelete"
OnClientClick="return confirm('Are you sure?')"/>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td><asp:Label ID="lblId" Text='<%#Eval("Id") %>' runat="server" /></td>
<td><asp:TextBox ID="txtName" runat="server" Width="120" Text='<%# Eval("Name") %>' /></td>
<td><asp:TextBox ID="txtCountry" runat="server" Width="120" Text='<%# Eval("Country") %>' /></td>
<td><asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" OnClick="OnUpdate" /></td>
<td><asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel" OnClick="OnCancel" /></td>
</tr>
</EditItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
Namespaces
C#
using System.Data
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Country")
});
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
ViewState["Customers"] = dt;
this.BindDatalist();
}
}
protected void OnDelete(object sender, EventArgs e)
{
DataListItem item = (sender as LinkButton).NamingContainer as DataListItem;
DataTable dt = ViewState["Customers"] as DataTable;
dt.Rows[item.ItemIndex].Delete();
ViewState["Customers"] = dt;
this.BindDatalist();
}
protected void OnEdit(object sender, EventArgs e)
{
DataListItem item = (sender as LinkButton).NamingContainer as DataListItem;
dlCustomers.EditItemIndex = item.ItemIndex;
this.BindDatalist();
}
protected void OnUpdate(object sender, EventArgs e)
{
DataListItem item = (sender as LinkButton).NamingContainer as DataListItem;
DataTable dt = (DataTable)ViewState["Customers"];
string country = (item.FindControl("txtCountry") as TextBox).Text;
string CustomerName = (item.FindControl("txtCustomerName") as TextBox).Text;
dt.Rows[item.ItemIndex]["CustomerName"] = CustomerName;
dt.Rows[item.ItemIndex]["Country"] = country;
ViewState["Customers"] = dt;
dlCustomers.EditItemIndex = -1;
this.BindDatalist();
}
protected void OnCancel(object sender, EventArgs e)
{
dlCustomers.EditItemIndex = -1;
this.BindDatalist();
}
private void BindDatalist()
{
dlCustomers.DataSource = (DataTable)ViewState["Customers"];
dlCustomers.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"),
New DataColumn("Name"),
New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
ViewState("Customers") = dt
Me.BindDatalist()
End If
End Sub
Protected Sub OnDelete(ByVal sender As Object, ByVal e As EventArgs)
Dim item As DataListItem = TryCast((TryCast(sender, LinkButton)).NamingContainer, DataListItem)
Dim dt As DataTable = TryCast(ViewState("Customers"), DataTable)
dt.Rows(item.ItemIndex).Delete()
ViewState("Customers") = dt
Me.BindDatalist()
End Sub
Protected Sub OnEdit(ByVal sender As Object, ByVal e As EventArgs)
Dim item As DataListItem = TryCast((TryCast(sender, LinkButton)).NamingContainer, DataListItem)
dlCustomers.EditItemIndex = item.ItemIndex
Me.BindDatalist()
End Sub
Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
Dim item As DataListItem = TryCast((TryCast(sender, LinkButton)).NamingContainer, DataListItem)
Dim dt As DataTable = CType(ViewState("Customers"), DataTable)
Dim country As String = (TryCast(item.FindControl("txtCountry"), TextBox)).Text
Dim CustomerName As String = (TryCast(item.FindControl("txtCustomerName"), TextBox)).Text
dt.Rows(item.ItemIndex)("CustomerName") = CustomerName
dt.Rows(item.ItemIndex)("Country") = country
ViewState("Customers") = dt
dlCustomers.EditItemIndex = -1
Me.BindDatalist()
End Sub
Protected Sub OnCancel(ByVal sender As Object, ByVal e As EventArgs)
dlCustomers.EditItemIndex = -1
Me.BindDatalist()
End Sub
Private Sub BindDatalist()
dlCustomers.DataSource = CType(ViewState("Customers"), DataTable)
dlCustomers.DataBind()
End Sub
Screenshot