Bind ListView Control In ASP.NET using C#

We will bind some data from the database and we are using SQLDataSource.

Initial chamber

Step 1: Open Visual Studio 2010 and create an empty website. Give a suitable name Listview_demo.

Step 2: In Solution Explorer you will get your empty website. Add a web form, SQL Database. By going like the following:

For Web Form:

Listview_demo (Your Empty Website) - Right Click, Add New Item, then Web Form. Name it Listview_demo.aspx.

For SQL Server Database:

Listview_demo (Your Empty Website) - Right Click, Add New Item, then SQL Server Database. Add Database inside the App_Data_folder.

Database chamber

Step 3: Go to your database Database.mdf, we will create a table - tbl_Data. Go to database.mdf - Table and add New table. Design your table like the following:

Table - tbl_data (Don’t forget to make ID, Identity Specification - Yes)

table design

Show Table Data


table

Design chamber

Step 5: Now open your Listview_demo.aspx file, where we create our design for BINDING ListView using SQLDataSource.

ListView_demo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.       
  13.         <asp:ListView ID="ListView1" runat="server" DataKeyNames="Product ID"   
  14.             DataSourceID="SqlDataSource1">  
  15.             <AlternatingItemTemplate>  
  16.                 <li style="">Product ID:  
  17.                     <asp:Label ID="Product_IDLabel" runat="server"   
  18.                         Text='<%# Eval("[Product ID]") %>' />  
  19.                     <br />  
  20.                     Product Name:  
  21.                     <asp:Label ID="Product_NameLabel" runat="server"   
  22.                         Text='<%# Eval("[Product Name]") %>' />  
  23.                     <br />  
  24.                     Quantity:  
  25.                     <asp:Label ID="QuantityLabel" runat="server" Text='<%# Eval("Quantity") %>' />  
  26.                     <br />  
  27.                     Unit Price:  
  28.                     <asp:Label ID="Unit_PriceLabel" runat="server"   
  29.                         Text='<%# Eval("[Unit Price]") %>' />  
  30.                     <br />  
  31.                 </li>  
  32.             </AlternatingItemTemplate>  
  33.             <EditItemTemplate>  
  34.                 <li style="">Product ID:  
  35.                     <asp:Label ID="Product_IDLabel1" runat="server"   
  36.                         Text='<%# Eval("[Product ID]") %>' />  
  37.                     <br />  
  38.                     Product Name:  
  39.                     <asp:TextBox ID="Product_NameTextBox" runat="server"   
  40.                         Text='<%# Bind("[Product Name]") %>' />  
  41.                     <br />  
  42.                     Quantity:  
  43.                     <asp:TextBox ID="QuantityTextBox" runat="server"   
  44.                         Text='<%# Bind("Quantity") %>' />  
  45.                     <br />  
  46.                     Unit Price:  
  47.                     <asp:TextBox ID="Unit_PriceTextBox" runat="server"   
  48.                         Text='<%# Bind("[Unit Price]") %>' />  
  49.                     <br />  
  50.                     <asp:Button ID="UpdateButton" runat="server" CommandName="Update"   
  51.                         Text="Update" />  
  52.                     <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"   
  53.                         Text="Cancel" />  
  54.                 </li>  
  55.             </EditItemTemplate>  
  56.             <EmptyDataTemplate>  
  57.                 No data was returned.  
  58.             </EmptyDataTemplate>  
  59.             <InsertItemTemplate>  
  60.                 <li style="">Product Name:  
  61.                     <asp:TextBox ID="Product_NameTextBox" runat="server"   
  62.                         Text='<%# Bind("[Product Name]") %>' />  
  63.                     <br />  
  64.                     Quantity:  
  65.                     <asp:TextBox ID="QuantityTextBox" runat="server"   
  66.                         Text='<%# Bind("Quantity") %>' />  
  67.                     <br />  
  68.                     Unit Price:  
  69.                     <asp:TextBox ID="Unit_PriceTextBox" runat="server"   
  70.                         Text='<%# Bind("[Unit Price]") %>' />  
  71.                     <br />  
  72.                     <asp:Button ID="InsertButton" runat="server" CommandName="Insert"   
  73.                         Text="Insert" />  
  74.                     <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"   
  75.                         Text="Clear" />  
  76.                 </li>  
  77.             </InsertItemTemplate>  
  78.             <ItemSeparatorTemplate>  
  79.                 <br />  
  80.             </ItemSeparatorTemplate>  
  81.             <ItemTemplate>  
  82.                 <li style="">Product ID:  
  83.                     <asp:Label ID="Product_IDLabel" runat="server"   
  84.                         Text='<%# Eval("[Product ID]") %>' />  
  85.                     <br />  
  86.                     Product Name:  
  87.                     <asp:Label ID="Product_NameLabel" runat="server"   
  88.                         Text='<%# Eval("[Product Name]") %>' />  
  89.                     <br />  
  90.                     Quantity:  
  91.                     <asp:Label ID="QuantityLabel" runat="server" Text='<%# Eval("Quantity") %>' />  
  92.                     <br />  
  93.                     Unit Price:  
  94.                     <asp:Label ID="Unit_PriceLabel" runat="server"   
  95.                         Text='<%# Eval("[Unit Price]") %>' />  
  96.                     <br />  
  97.                 </li>  
  98.             </ItemTemplate>  
  99.             <LayoutTemplate>  
  100.                 <ul ID="itemPlaceholderContainer" runat="server" style="">  
  101.                     <li runat="server" id="itemPlaceholder" />  
  102.                 </ul>  
  103.                 <div style="">  
  104.                 </div>  
  105.             </LayoutTemplate>  
  106.             <SelectedItemTemplate>  
  107.                 <li style="">Product ID:  
  108.                     <asp:Label ID="Product_IDLabel" runat="server"   
  109.                         Text='<%# Eval("[Product ID]") %>' />  
  110.                     <br />  
  111.                     Product Name:  
  112.                     <asp:Label ID="Product_NameLabel" runat="server"   
  113.                         Text='<%# Eval("[Product Name]") %>' />  
  114.                     <br />  
  115.                     Quantity:  
  116.                     <asp:Label ID="QuantityLabel" runat="server" Text='<%# Eval("Quantity") %>' />  
  117.                     <br />  
  118.                     Unit Price:  
  119.                     <asp:Label ID="Unit_PriceLabel" runat="server"   
  120.                         Text='<%# Eval("[Unit Price]") %>' />  
  121.                     <br />  
  122.                 </li>  
  123.             </SelectedItemTemplate>  
  124.         </asp:ListView>  
  125.         <asp:SqlDataSource ID="SqlDataSource1" runat="server"   
  126.             ConnectionString="<%$ ConnectionStrings:ConnectionString %>"   
  127.             SelectCommand="SELECT * FROM [tbl_data]"></asp:SqlDataSource>  
  128.       
  129.     </div>  
  130.     </form>  
  131. </body>  
  132. </html>  
Output chamber

listview from database

Hope you liked this. Thank you for reading.

 

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now