Data Controls In ASP.NET

Introduction

Both DataList and DetailsView are Data Controls, means both are used to Display and Manipulate Data.

Note: The Controls having DataSource Property are called Data Controls in ASP.NET.

Data List:

  • DataList is an Unformatted Data Control like repeater control in ASP.NET.
  • DataList Controls are used to display a list of Items.
  • The DataList control is useful for displaying data in any repeating structure.
  • Eval() and Bind() can be used to bind data in DataList.

The following Templates can be used to display the Structure of DataList:

    <AlternatingItemTemplate>
    <edititemtemplate>
    <footertemplate>
    <headertemplate>
    <itemtemplate>
    <selecteditemtemplate>
    <separatortemplate>

Details View

  • DetailsView is a formatted data control like GridView Control in ASP.NET.
  • The DetailsView control displays only a single data record at a time, even if its data source exposes multiple records.
  • CRUD operations are possible in DetailsView.
  • asp:BoundField is used to bind data in DetailsView.

The following are the some important templates of DetailsView Control:

    <HeaderTemplate>
    <FooterTemplate>
    <PagerTemplate>
    <Fields>

Here is an example to display the contents of the Product database table in DataList and DetailsView:

Step 1: Create Database and Table to display in Controls

Create Database and Table

Step 2: Create Connection with the table using ADONET classes in code behind page.

Code behind Page

Step 3: In the aspx page, I have used some template to Bind and Display Data

  1. <div style="height: 100%; width: 100%;">  
  2.     <div style="height: 650px; width: 650px; float: left;">  
  3.         <%--Data List Control--%>  
  4.         <asp:DataList ID="DataList1" runat="server" RepeatColumns="3" BorderStyle="Double" BorderWidth="4px"  
  5.             CellPadding="3" CellSpacing="2" GridLines="Both" Height="617px">  
  6.             <HeaderTemplate>DataList Control</HeaderTemplate>  
  7.             <HeaderStyle BackColor="Gray" Font-Bold="true" HorizontalAlign="Center" Font-Size="X-Large" />  
  8.             <ItemTemplate>  
  9.                 <div>  
  10.                     <table style="width: 170px; text-align: center; border-color: red;">  
  11.                         <tr>  
  12.                             <td>  
  13.                                 <asp:Image ID="img" runat="server" ImageUrl='<%#Eval("ProductImages") %>' />  
  14.                             </td>  
  15.                         </tr>  
  16.                         <tr>  
  17.                             <td>  
  18.                                 <asp:Label ID="Label1" runat="server" Text='<%#Eval("ProductName") %>'></asp:Label>  
  19.                             </td>  
  20.                         </tr>  
  21.                         <tr>  
  22.                             <td>  
  23.                                 <asp:Label ID="Label2" runat="server" Text='<%#Eval("ProductPrice") %>'></asp:Label>  
  24.                             </td>  
  25.                         </tr>  
  26.                         <tr>  
  27.                             <td>  
  28.                                 <asp:Button ID="btnaddcrt" runat="server" Text="Add To Cart"></asp:Button>  
  29.                                 <asp:Button ID="Button1" runat="server" Text="Details"></asp:Button></td>  
  30.                         </tr>  
  31.                     </table>  
  32.                 </div>  
  33.             </ItemTemplate>  
  34.         </asp:DataList>  
  35.   
  36.     </div>  
  37.     <%--DetailsView Control:--%>  
  38.     <div style="float: left;">  
  39.         <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="false" Height="200px" Width="400px">  
  40.             <HeaderTemplate>DetailsView Control</HeaderTemplate>  
  41.             <HeaderStyle BackColor="LightBlue" Font-Bold="true" HorizontalAlign="Center" Font-Size="X-Large" />  
  42.             <Fields>  
  43.                 <asp:BoundField DataField="ProductType" HeaderText="Categorey" />  
  44.             </Fields>  
  45.             <Fields>  
  46.                 <asp:BoundField DataField="ProductName" HeaderText="Product Name" />  
  47.             </Fields>  
  48.             <Fields>  
  49.                 <asp:BoundField DataField="ProductPrice" HeaderText="Price" />  
  50.             </Fields>  
  51.         </asp:DetailsView>  
  52.     </div>  
  53. </div>  
Step 4: Added ProductImages folder in Solution Explorer and  now paste the image.

ProductImages

Output will look like the following screenshot:

Output

 


Similar Articles