The ListView control displays columns and rows of data and allows sorting and paging. It is by far the most popular data display control, and is ideal for understanding how data display controls interact with data retrieval controls and code.
Note: For interactions with a database like Create, Read, Update and Delete, you should have prior knowledge of SQL.
I will discuss following processes to bind ListView Control:
- SQL Server Express
- Microsoft SQL Server
1. Data Access in ListView Control by SQL Server Express
First create a new web page, if you don't know how to create a web page then just see the previous tutorial. Now switch to Design View and drag the ListView control from the Toolbox directly onto the design surface. See in Figure 1.
Figure 1
Now choose the data source over the ListView's right side. See in Figure 2.
Figure 2
Now you see the Data Source Configuration Wizard, here you can choose the Data Source type, like Access, SQL, Entity, LINQ etcetera. Then choose the SQL Database as you are familiar with. In the bottom specify an ID for the data source; by default it is SqlDataSource1; just choose this name or change it as per you prefer. See Figure 3.
Figure 3
Now choose your new Data Connection & Server name. See Figures 4 & 5.
Figure 4
Figure 5
In the Add Connection window, for example the server name is CAMPUSWO-1703D3. Here you can choose Windows Authentication or SQL Server Authentication.
After authentication, choose the database name. We will discuss another option in future tutorials. Now test your connection; if it is successful then proceed to the next section. See Figure 6.
Figure 6
Now you see your server name along with the database. See Figure 7.
Figure 7
Now you see a Connection String for SQL Server Express. See Figure 8.
Figure 8
Next, configure the database table or Stored Procedure from which the data will be retrieved. I have chosen the Categories table as an example. Here you can choose to order by or use advanced SQL commands. See Figure 9.
Figure 9
Now test the query and see the results. See Figure 10.
Figure 10
After finishing the test query, now see the source file in "default.aspx" as below:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListView.aspx.cs" Inherits="ListView"%>
<!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 id="Head2" runat="server">
<title></title>
</head>
<body>
<form id="form2" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:BlogEngineConnectionString %>"
SelectCommand="SELECT * FROM [Comments]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
But the code above is not finished until you make a design and data bound for the ListView. Now we will make a LayoutTemplate & ItemTemplate in a table then bind it with the columns of the SQL table named "Comments" as you see in Figure 10. Now see the following complete code.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListView.aspx.cs" Inherits="ListView"%>
<!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 id="Head1" runat="server">
<title>ListView Control - ASP.NET Tutorials</title>
<style type="text/css">
.TableCSS
{
border-style:none;
background-color:#3BA0D8;
width: 850px;
}
.TableHeader
{
background-color:#66CCFF;
color:#0066FF;
font-size:large;
font-family:Verdana;
}
.TableData
{
background-color:#82C13E;
color:#fff;
font-family:Courier New;
font-size:medium;
font-weight:bold;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:#3BA0D8; font-style:italic;">ListView Control Example in ASP.NET: How To Use ListView Control</h2>
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<LayoutTemplate>
<table id="Table1" runat="server" class="TableCSS">
<tr id="Tr1" runat="server" class="TableHeader">
<td id="Td1" runat="server">Comment ID</td>
<td id="Td2" runat="server">Blog ID</td>
<td id="Td3" runat="server">Date</td>
<td id="Td4" runat="server">Name</td>
<td id="Td5" runat="server">Comments</td>
</tr>
<tr id="ItemPlaceholder" runat="server">
</tr>
<tr id="Tr2" runat="server">
<td id="Td6" runat="server" colspan="2">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Link" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ButtonType="Link" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr class="TableData">
<td>
<asp:Label
ID="Label1"
runat="server"
Text='<%# Eval("id")%>'
>
</asp:Label>
</td>
<td>
<asp:Label
ID="Label2"
runat="server"
Text='<%# Eval("Blog_id")%>'>
</asp:Label>
</td>
<td>
<asp:Label
ID="Label3"
runat="server"
Text='<%# Eval("Date")%>'>
</asp:Label>
</td>
<td>
<asp:Label
ID="Label4"
runat="server"
Text='<%# Eval("Name")%>'>
</asp:Label>
</td>
<td>
<asp:Label
ID="Label5"
runat="server"
Text='<%# Eval("Comment")%>'>
</asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:BlogEngineConnectionString %>" SelectCommand="SELECT * FROM [Comments]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
Now finally see the output in a browser by pressing F6 (Build) and F5 (Debug). See Figure 11.
Figure 11
2. Data Access in ListView Control by SQL Server
Now we will learn how to fill a ListView by SQL Server queries and ASP.NET classes. First drag a ListView control in design view. See Figure 1.
Now by pressing F7 you will see code-behind files. We will write SQL Server queries and ASP.NET classes here. See Figure 12.
Figure 12
Here is the code-behind files in "default.aspx.cs".
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=(local);database=BlogEngine;Integrated Security=true;");
SqlDataAdapter da = new SqlDataAdapter("select * from Comments", con);
DataSet ds = new DataSet();
da.Fill(ds, "FillComent");
ListView1.DataSource = ds.Tables["FillComent "];
ListView1.DataBind();
}
}