Update Data in Datalist Using Template

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:

  1. <p class=" " style="margin-bottom: 0.0001pt;">  
  2.     <asp:DataList ID="DataList1" runat="server" Width="100%" OnCancelCommand="DataList1_CancelCommand"  
  3.         OnEditCommand="DataList1_EditCommand" 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%">  
  11.                     Address1:  
  12.                 </td>  
  13.                 <td>  
  14.                     <%#Eval("Address1")%>  
  15.                 </td>  
  16.             </tr>  
  17.             <tr>  
  18.                 <td>  
  19.                     Address2:  
  20.                 </td>  
  21.                 <td>  
  22.                     <%#Eval("Address2")%>  
  23.                 </td>  
  24.             </tr>  
  25.             <tr>  
  26.                 <td style="width: 30%">  
  27.                     City:  
  28.                 </td>  
  29.                 <td>  
  30.                     <%#Eval("City")%>  
  31.                 </td>  
  32.             </tr>  
  33.             <tr>  
  34.                 <td>  
  35.                     State:  
  36.                 </td>  
  37.                 <td>  
  38.                     <%#Eval("State")%>  
  39.                 </td>  
  40.             </tr>  
  41.             <tr>  
  42.                 <td>  
  43.                     Pin Code:  
  44.                 </td>  
  45.                 <td>  
  46.                     <%#Eval("PIN")%>  
  47.                 </td>  
  48.             </tr>  
  49.             <tr>  
  50.                 <td>  
  51.                 </td>  
  52.                 <td>  
  53.                     <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>  
  54.                 </td>  
  55.             </tr>  
  56.         </ItemTemplate>  
  57.         <EditItemTemplate>  
  58.             <tr>  
  59.                 <td style="width: 10%">  
  60.                     Address1:  
  61.                 </td>  
  62.                 <td>  
  63.                     <asp:TextBox ID="txtAdd1" runat="server" Width="200px" Text='<%#Eval("Address1") %>'></asp:TextBox>  
  64.                 </td>  
  65.             </tr>  
  66.             <tr>  
  67.                 <td>  
  68.                     Address2:  
  69.                 </td>  
  70.                 <td>  
  71.                     <asp:TextBox ID="txtAdd2" runat="server" Width="200px" Text='<%#Eval("Address2") %>'></asp:TextBox>  
  72.                 </td>  
  73.             </tr>  
  74.             <tr>  
  75.                 <td style="width: 30%">  
  76.                     City:  
  77.                 </td>  
  78.                 <td>  
  79.                     <asp:TextBox ID="txtCity" runat="server" Width="200px" Text='<%#Eval("City") %>'></asp:TextBox>  
  80.                 </td>  
  81.             </tr>  
  82.             <tr>  
  83.                 <td>  
  84.                     State:  
  85.                 </td>  
  86.                 <td>  
  87.                     <asp:TextBox ID="txtState" runat="server" Width="200px" Text='<%#Eval("State") %>'></asp:TextBox>  
  88.                 </td>  
  89.             </tr>  
  90.             <tr>  
  91.                 <td>  
  92.                     Pin Code:  
  93.                 </td>  
  94.                 <td>  
  95.                     <asp:TextBox ID="txtPin" runat="server" Text='<%#Eval("PIN") %>'></asp:TextBox>  
  96.                     <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Invalide Pin"  
  97.                         ControlToValidate="txtPin" ValidationExpression="\d{6}">*</asp:RegularExpressionValidator>  
  98.                 </td>  
  99.             </tr>  
  100.             <tr>  
  101.                 <td>  
  102.                 </td>  
  103.                 <td>  
  104.                     <asp:Button ID="btnsave" runat="server" Text="Update" EnableViewState="False" Font-Italic="True"  
  105.                         CommandName="Update"></asp:Button>     
  106.                     <asp:Button ID="btncancel" runat="server" Text="Cancel" EnableViewState="False" Font-Italic="True"  
  107.                         CommandName="Cancel"></asp:Button>  
  108.                 </td>  
  109.             </tr>  
  110.         </EditItemTemplate>  
  111.         <FooterTemplate>  
  112.             </table>  
  113.         </FooterTemplate>  
  114.     </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:

  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. }

Use the 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 the databse table in the editcommand event of the DataList control; see:

  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. }

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.


Similar Articles