Introduction

In this article, I will demonstrate how to dynamically bind ListView control in ASP.NET from a database and display the values of a data source by using user-defined templates. The ListView control enables users to select, sort, delete, edit, and insert records.

Step 1

Create a database table in SQL Server 2014.

  1. CREATE TABLE [dbo].[Customers](
  2. [CustomerID] [int] IDENTITY(1,1) NOT NULL,
  3. [CustomerName] [nvarchar](50) NULL,
  4. [Gender] [char](10) NULL,
  5. [City] [nvarchar](50) NULL,
  6. [State] [nvarchar](50) NULL,
  7. [Country] [nvarchar](50) NULL,
  8. CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
  9. (
  10. [CustomerID] ASC
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  12. ) ON [PRIMARY]
  13. GO
  1. CREATE PROCEDURE [dbo].[spGetAllCustomers]
  2. AS
  3. BEGIN
  4. SELECT CustomerID,CustomerName,Gender,City,State,Country from [dbo].[Customers]
  5. END

Screenshot of database table

ASP.NET

Step 2

Open Visual Studio 2015, click on New Project, and create an empty web application project.

Screenshot for creating new project 1

ASP.NET
After clicking on New Project, one window will appear. Select Web from the left panel, choose ASP.NET Web Application, give a meaningful name of your project and then click on OK as shown in below screenshot.

Screenshot for creating new project 2

ASP.NET

After clicking on OK, one more window will appear. Choose Empty check on Web Forms checkbox and click on OK.

Screenshot for creating new project 3

ASP.NET

Step 3

Double click on webconfig file and database connect. Write the following line of code.

  1. <connectionStrings>
  2. <add name="DBCS" connectionString="data source=DESKTOP-M021QJH\SQLEXPRESS; database=DemoDB; integrated security=true;"/>
  3. </connectionStrings>

Step 4

Add web form in your project.

Right click on project select add choose Web Form and click.

Screenshot adding web form 1

ASP.NET
After clicking on web form one window will appear, give a name to web form such as ListView or any meaningful name. Click on OK and web form will be added in project.

Screenshot adding web form 2

ASP.NET

Step 5

Add script and styles of bootstrap in head section of web form.

  1. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  3. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>

Step 6

Drag and drop a ListView control on web form. Design ListView control.

  1. <body>
  2. <form id="form1" runat="server">
  3. <div class="container py-4">
  4. <h5 class="text-uppercase text-center">ListView control in asp.net</h5>
  5. <asp:ListView ID="ListView1" runat="server">
  6. <LayoutTemplate>
  7. <table class="table table-bordered table-striped">
  8. <tr class="bg-danger text-white">
  9. <th>Customer ID</th>
  10. <th>Customer Name</th>
  11. <th>Gender</th>
  12. <th>City</th>
  13. <th>State</th>
  14. <th>Country</th>
  15. </tr>
  16. <tbody>
  17. <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
  18. </tbody>
  19. </table>
  20. </LayoutTemplate>
  21. <ItemTemplate>
  22. <tr>
  23. <td><%# Eval("CustomerID")%></td>
  24. <td><%# Eval("CustomerName")%></td>
  25. <td><%# Eval("Gender")%></td>
  26. <td><%# Eval("City")%></td>
  27. <td><%# Eval("State")%></td>
  28. <td><%# Eval("Country")%></td>
  29. </tr>
  30. </ItemTemplate>
  31. </asp:ListView>
  32. </div>
  33. </form>
  34. </body>

Complete web form code

  1. <!DOCTYPE html>
  2. <html>
  3. <head runat="server">
  4. <title>ListView</title>
  5. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div class="container py-4">
  12. <h5 class="text-uppercase text-center">ListView control in asp.net</h5>
  13. <asp:ListView ID="ListView1" runat="server">
  14. <LayoutTemplate>
  15. <table class="table table-bordered table-striped">
  16. <tr class="bg-danger text-white">
  17. <th>Customer ID</th>
  18. <th>Customer Name</th>
  19. <th>Gender</th>
  20. <th>City</th>
  21. <th>State</th>
  22. <th>Country</th>
  23. </tr>
  24. <tbody>
  25. <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
  26. </tbody>
  27. </table>
  28. </LayoutTemplate>
  29. <ItemTemplate>
  30. <tr>
  31. <td><%# Eval("CustomerID")%></td>
  32. <td><%# Eval("CustomerName")%></td>
  33. <td><%# Eval("Gender")%></td>
  34. <td><%# Eval("City")%></td>
  35. <td><%# Eval("State")%></td>
  36. <td><%# Eval("Country")%></td>
  37. </tr>
  38. </ItemTemplate>
  39. </asp:ListView>
  40. </div>
  41. </form>
  42. </body>
  43. </html>

Step 7

Right click and view web form code. Write the following code.

  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. namespace BindDataControl_Demo
  6. {
  7. public partial class ListView : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. if (!IsPostBack)
  12. {
  13. BindListView();
  14. }
  15. }
  16. private void BindListView()
  17. {
  18. string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
  19. using (SqlConnection con = new SqlConnection(CS))
  20. {
  21. SqlCommand cmd = new SqlCommand("spGetAllCustomers", con);
  22. cmd.CommandType = CommandType.StoredProcedure;
  23. con.Open();
  24. ListView1.DataSource = cmd.ExecuteReader();
  25. ListView1.DataBind();
  26. }
  27. }
  28. }
  29. }

Step 8

Run the project ctrl+F5

Screenshot

ASP.NET

Conclusion

In this article I have explained how to dynamically bind ListView control from database step by step. I hope it will help you.