Introduction
Updating data in a DataList is a very easy task in ASP.NET. I have use the Entity Framework to insert, update and delete data from a DataList.
Background
I have used templates to store data in the DataList, like label, TextBox and a button for raising events.
The DataList in my page is like the following.
Using the code
Drag one DataList Control from the Toolbox and Declare its ItemTemplet like this given below:
<p class=" " style="margin-bottom: 0.0001pt;">
<asp:DataList ID="DataList1" runat="server" Width="100%" OnCancelCommand="DataList1_CancelCommand"
OnEditCommand="DataList1_EditCommand" OnItemCommand="DataList1_ItemCommand" OnItemDataBound="DataList1_ItemDataBound"
OnUpdateCommand="DataList1_UpdateCommand">
<HeaderTemplate>
<table width="100%">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width: 30%">
Address1:
</td>
<td>
<%#Eval("Address1")%>
</td>
</tr>
<tr>
<td>
Address2:
</td>
<td>
<%#Eval("Address2")%>
</td>
</tr>
<tr>
<td style="width: 30%">
City:
</td>
<td>
<%#Eval("City")%>
</td>
</tr>
<tr>
<td>
State:
</td>
<td>
<%#Eval("State")%>
</td>
</tr>
<tr>
<td>
Pin Code:
</td>
<td>
<%#Eval("PIN")%>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td style="width: 10%">
Address1:
</td>
<td>
<asp:TextBox ID="txtAdd1" runat="server" Width="200px" Text='<%#Eval("Address1") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
Address2:
</td>
<td>
<asp:TextBox ID="txtAdd2" runat="server" Width="200px" Text='<%#Eval("Address2") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 30%">
City:
</td>
<td>
<asp:TextBox ID="txtCity" runat="server" Width="200px" Text='<%#Eval("City") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
State:
</td>
<td>
<asp:TextBox ID="txtState" runat="server" Width="200px" Text='<%#Eval("State") %>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
Pin Code:
</td>
<td>
<asp:TextBox ID="txtPin" runat="server" Text='<%#Eval("PIN") %>'></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Invalide Pin"
ControlToValidate="txtPin" ValidationExpression="\d{6}">*</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnsave" runat="server" Text="Update" EnableViewState="False" Font-Italic="True"
CommandName="Update"></asp:Button>   
<asp:Button ID="btncancel" runat="server" Text="Cancel" EnableViewState="False" Font-Italic="True"
CommandName="Cancel"></asp:Button>
</td>
</tr>
</EditItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
On the page event bind your DataList with Database, I have used The Entity Framework to bind the database to the code; it is given below:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["id"]== null)
{
using (OCSEntities ocs = new OCSEntities())
{
ocs.Connection.Open();
DataList1.DataSource = ocs.Addresses.Where(item => item.UID == SessionManager.Current.CurrentUser.ID).ToList();
DataList1.DataBind();
}
}
else
{
DataList1.Visible = false;
pnlnew.Visible = true;
}
}
Use the ItemCommand Event to specify the Edit item index of the DataList.
For example:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
DataList1.EditItemIndex = 0;
}
}
Again rebind your DataList to the databse table in the editcommand event of the DataList control; see:
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
//DataList1.EditItemIndex = 0;
using (OCSEntities ocs = new OCSEntities())
{
ocs.Connection.Open();
DataList1.DataSource = ocs.Addresses.Where(item => item.UID == SessionManager.Current.CurrentUser.ID).ToList();
DataList1.DataBind();
}
}
Use the updatecommand event to update your database as in the following:
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
using (OCSEntities ocs = new OCSEntities())
{
ocs.Connection.Open();
OCSModel.Address ad = ocs.Addresses.Where(item => item.UID == SessionManager.Current.CurrentUser.ID).First();
ad.Address1 = ((TextBox)e.Item.FindControl("txtAdd1")).Text;
ad.Address2 = ((TextBox)e.Item.FindControl("txtAdd2")).Text;
ad.City = ((TextBox)e.Item.FindControl("txtCity")).Text;
ad.State = ((TextBox)e.Item.FindControl("txtState")).Text;
ad.PIN = ((TextBox)e.Item.FindControl("txtPin")).Text;
ocs.SaveChanges();
ocs.AcceptAllChanges();
}
DataList1.EditItemIndex = -1;
using (OCSEntities ocs = new OCSEntities())
{
ocs.Connection.Open();
DataList1.DataSource = ocs.Addresses.Where(item => item.UID == SessionManager.Current.CurrentUser.ID).ToList();
DataList1.DataBind();
}
}
}
Points of Interest
Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany?
History
Keep a running update of any changes or improvements you've made here.