ListView control makes data binding easier than previous controls. It has included styling with CSS, flexible pagination, and sorting, inserting, deleting, and updating features. In this article we create a SQL server database table with ID, title, description and author name. Drag and drop a ListView control on the asp.net form to bind records with database.
Templates available with ListView.
1. LayoutTemplate.
2. ItemTemplate.
3. AlternatingItemTemplate
LayoutTemplate
LayoutTemplate is used to show the overall layout of the template. We can give the title for our page. We need to place a placeholder the id of this control specifically should be "itemPlaceholder".
ItemTemplate
ItemTemplate is visual layout of the each item that we iterated over the each element inside the ListView Control. Specify the individual items that you want to display in the ListView.
AlternatingItemTemplate - Defines how alternating items are displayed.
Creating a Table
Create a table in database named as Articletable. Table looks like this.
CREATE TABLE [dbo].[Articletable](
[ID] [int] NOT NULL,
[Title] [varchar](200) NULL,
[Description] [varchar](400) NULL,
[Author] [varchar](50) NULL
)
Inserting data in the articletable.
INSERT INTO Articletable VALUES(1,'How to validate dropdownlist in asp.net','Here, we will learn how to validate a DropDownList in ASP.NET.','Rohatash Kumar');
GO
INSERT INTO Articletable VALUES(2,'Introduction to .NET Assemblies in VB.NET',' Here is a comprehensive introduction to .NET assemblies.','sunil Kumar');
go
INSERT INTO Articletable VALUES(3,'BinaryReader and BinaryWriter classes in VB.NET','In this article I will explain about BinaryReader and BinaryWriter Classes in VB.NET.','Deepak Kumar');
go
INSERT INTO Articletable VALUES(4,'StreamWriter class in VB.NET','This article shows how to create a new text file and write a string to it.','Rohatash Kumar');
go
select * from articletable;
OUTPUT
Now drag and drop a ListView control from the Toolbox on the Form. The form looks like this.
ListView1
The ASP. NET code for the ListView.aspx page.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<tr style="background-color:#DCDCDC;color: #000000;">
<td>
<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
</td>
<td>
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
</td>
<td>
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
</td>
<td>
<asp:Label ID="AuthorLabel" runat="server" Text='<%# Eval("Author") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="1"
style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr runat="server" style="background-color:#DCDCDC;color: #000000;">
<th runat="server">
ID</th>
<th runat="server">
Title</th>
<th runat="server">
Description</th>
<th runat="server">
Author</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server"
style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;">
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:userinfoConnectionString2 %>"
SelectCommand="SELECT * FROM [Articletable]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
Now run the application and test it.
ListView2