Update Data in DataList Using Template With EntityFramework in ASP.Net 4.0

Introduction

This article provides an example of how to update a DataList in ASP.Net. This code example updates data using the Entity Framework. Updating data in a DataList is very easy in ASP.Net, I have used the Entity Framework to Insert, Update and Delete data from a DataList.

Background

For example, a DataList containing a Label, TextBox and Button as a template for displaying and raising events. The DataList in my page is as given below, to do this using the code Drag one DataList Control from a Toolbox and declare their ItemTemplate as given below.

HTML Code

  1. <p class=" "style="margin-bottom: 0.0001pt;"><asp:DataListID="DataList1" runat="server" width="100%"  
  2. oncancelcommand="DataList1_CancelCommand" oneditcommand="DataList1_EditCommand"  
  3. onitemcommand="DataList1_ItemCommand" onitemdatabound="DataList1_ItemDataBound"  
  4. onupdatecommand="DataList1_UpdateCommand">  
  5. <HeaderTemplate>  
  6. <table width="100%">  
  7. </HeaderTemplate>  
  8. <ItemTemplate>  
  9. <tr>  
  10. <td style="width:30%">Address1:</td>  
  11. <td><%#Eval("Address1")%></td>  
  12. </tr>  
  13. <tr>  
  14. <td>Address2:</td>  
  15. <td><%#Eval("Address2")%></td>  
  16. </tr>  
  17. <tr>  
  18. <td style="width:30%">City:</td>  
  19. <td><%#Eval("City")%></td>  
  20. </tr>  
  21. <tr>  
  22. <td>State:</td>  
  23. <td><%#Eval("State")%></td>  
  24. </tr>  
  25. <tr>  
  26. <td>Pin Code:</td>  
  27. <td><%#Eval("PIN")%></td>  
  28. </tr>  
  29. <tr>  
  30. <td>  
  31. </td>  
  32. <td><asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton></td>  
  33. </tr>  
  34. </ItemTemplate>  
  35. <EditItemTemplate>  
  36. <tr>  
  37. <td style="width:10%">Address1:</td>  
  38. <td><asp:TextBox ID="txtAdd1" runat="server" Width="200px" Text='<%#Eval("Address1") %>'></asp:TextBox></td>  
  39. </tr>  
  40. <tr>  
  41. <td>Address2:</td>  
  42. <td><asp:TextBox ID="txtAdd2" runat="server" Width="200px" Text='<%#Eval("Address2") %>'></asp:TextBox></td>  
  43. </tr>  
  44. <tr>  
  45. <td style="width:30%">City:</td>  
  46. <td><asp:TextBox ID="txtCity" runat="server" Width="200px" Text='<%#Eval("City") %>'></asp:TextBox></td>  
  47. </tr>  
  48. <tr>  
  49. <td>State:</td>  
  50. <td><asp:TextBox ID="txtState" runat="server" Width="200px" Text='<%#Eval("State") %>'></asp:TextBox></td>  
  51. </tr>  
  52. <tr>  
  53. <td>Pin Code:</td>  
  54. <td><asp:TextBox ID="txtPin" runat="server" Text='<%#Eval("PIN") %>'></asp:TextBox>  
  55. <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"  
  56. ErrorMessage="Invalide Pin" ControlToValidate="txtPin"  
  57. ValidationExpression="\d{6}">*</asp:RegularExpressionValidator>  
  58. </td>  
  59. </tr>  
  60. <tr>  
  61. <td>  
  62. </td>  
  63. <td><asp:Button ID="btnsave" runat="server" Text="Update" EnableViewState="False"  
  64. Font-Italic="True" CommandName="Update"></asp:Button>     
  65. <asp:Button ID="btncancel" runat="server" Text="Cancel" EnableViewState="False" Font-Italic="True" CommandName="Cancel"></asp:Button>  
  66. </td>  
  67. </tr>  
  68. </EditItemTemplate>  
  69. <FooterTemplate>  
  70. </table>  
  71. </FooterTemplate>  
  72. </asp:DataList>  
On the page load event bind your DataList with the database. I have used the Entity Framework to bind to the database as in the following:

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!IsPostBack)  
  4.     {  
  5.         if (Request.QueryString["id"] == null)  
  6.         {  
  7.             using (OCSEntities ocs = new OCSEntities())  
  8.             {  
  9.                 ocs.Connection.Open();  
  10.                 DataList1.DataSource = ocs.Addresses.Where(item => item.UID == SessionManager.Current.CurrentUser.ID).ToList();  
  11.                 DataList1.DataBind();  
  12.             }  
  13.         }  
  14.         else  
  15.         {  
  16.             DataList1.Visible = false;  
  17.             pnlnew.Visible = true;  
  18.         }  
  19.     }  
  20. }   

Use an ItemCommand Event to specify the Edit item index of the DataList.

For example:

  1. protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)  
  2. {  
  3.     if (e.CommandName == "Edit")  
  4.     {  
  5.         DataList1.EditItemIndex = 0;  
  6.     }  
  7. }   

Again, rebind your DataList to a database table in the editcommand event of the DataList control:

  1. protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)  
  2. {  
  3.     //DataList1.EditItemIndex = 0;  
  4.     using (OCSEntities ocs = new OCSEntities())  
  5.     {  
  6.         ocs.Connection.Open();  
  7.         DataList1.DataSource = ocs.Addresses.Where(item => item.UID == SessionManager.Current.CurrentUser.ID).ToList();  
  8.         DataList1.DataBind();  
  9.     }  
  10. }   

Use the updatecommand event to update your database as in the following:

  1. protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)  
  2. {  
  3.     if (e.Item.ItemType == ListItemType.EditItem)  
  4.     {  
  5.         using (OCSEntities ocs = new OCSEntities())  
  6.         {  
  7.             ocs.Connection.Open();  
  8.             OCSModel.Address ad = ocs.Addresses.Where(item => item.UID == SessionManager.Current.CurrentUser.ID).First();  
  9.             ad.Address1 = ((TextBox)e.Item.FindControl("txtAdd1")).Text;  
  10.             ad.Address2 = ((TextBox)e.Item.FindControl("txtAdd2")).Text;  
  11.             ad.City = ((TextBox)e.Item.FindControl("txtCity")).Text;  
  12.             ad.State = ((TextBox)e.Item.FindControl("txtState")).Text;  
  13.             ad.PIN = ((TextBox)e.Item.FindControl("txtPin")).Text;  
  14.             ocs.SaveChanges();  
  15.             ocs.AcceptAllChanges();  
  16.         }  
  17.         DataList1.EditItemIndex = -1;  
  18.         using (OCSEntities ocs = new OCSEntities())  
  19.         {  
  20.             ocs.Connection.Open();  
  21.             DataList1.DataSource = ocs.Addresses.Where(item => item.UID == SessionManager.Current.CurrentUser.ID).ToList();  
  22.             DataList1.DataBind();  
  23.         }  
  24.     }  
  25. }   

Summary

In the preceding example, we demonstrated how to use a template in the DataList box and update the database using the Entity Framework.


Similar Articles