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:
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
Step 2: Create Connection with the table using ADONET classes in code behind page.
Step 3: In the aspx page, I have used some template to Bind and Display Data
- <div style="height: 100%; width: 100%;">
- <div style="height: 650px; width: 650px; float: left;">
- <%--Data List Control--%>
- <asp:DataList ID="DataList1" runat="server" RepeatColumns="3" BorderStyle="Double" BorderWidth="4px"
- CellPadding="3" CellSpacing="2" GridLines="Both" Height="617px">
- <HeaderTemplate>DataList Control</HeaderTemplate>
- <HeaderStyle BackColor="Gray" Font-Bold="true" HorizontalAlign="Center" Font-Size="X-Large" />
- <ItemTemplate>
- <div>
- <table style="width: 170px; text-align: center; border-color: red;">
- <tr>
- <td>
- <asp:Image ID="img" runat="server" ImageUrl='<%#Eval("ProductImages") %>' />
- </td>
- </tr>
- <tr>
- <td>
- <asp:Label ID="Label1" runat="server" Text='<%#Eval("ProductName") %>'></asp:Label>
- </td>
- </tr>
- <tr>
- <td>
- <asp:Label ID="Label2" runat="server" Text='<%#Eval("ProductPrice") %>'></asp:Label>
- </td>
- </tr>
- <tr>
- <td>
- <asp:Button ID="btnaddcrt" runat="server" Text="Add To Cart"></asp:Button>
- <asp:Button ID="Button1" runat="server" Text="Details"></asp:Button></td>
- </tr>
- </table>
- </div>
- </ItemTemplate>
- </asp:DataList>
-
- </div>
- <%--DetailsView Control:--%>
- <div style="float: left;">
- <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="false" Height="200px" Width="400px">
- <HeaderTemplate>DetailsView Control</HeaderTemplate>
- <HeaderStyle BackColor="LightBlue" Font-Bold="true" HorizontalAlign="Center" Font-Size="X-Large" />
- <Fields>
- <asp:BoundField DataField="ProductType" HeaderText="Categorey" />
- </Fields>
- <Fields>
- <asp:BoundField DataField="ProductName" HeaderText="Product Name" />
- </Fields>
- <Fields>
- <asp:BoundField DataField="ProductPrice" HeaderText="Price" />
- </Fields>
- </asp:DetailsView>
- </div>
- </div>
Step 4: Added
ProductImages folder in Solution Explorer and now paste the image.
Output will look like the following screenshot: