Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to bind data to ListView control in ASP.NET

Author: Richard Wand
Download Source Code : 1319_BindDataToListView.zip

This article will explain ListView control, templates used in Listview control and how we can bind data to ListView control in ASP.NET.

 

ListView Control
 
ListView control is similar to DataList, Repeater and GridView controls because it displays data in a repeating format from data source but it is also different from these data controls. ListView control provides full support for data source components. It is a template driven control that gives you full control to define layout and user interface with templates. ListView control provides easier data binding, flexible pagination and styling. It also support DataPager control which us specially designed for pagination. ListView is the control in ASP.NET that can be replaced with any other data-bound control and it will provide better results.
 
ListView Templates
 
In ListView control, items are displayed using templates. Items can be displayed individually or in groups.
Some ListView template are explained below.  
 
LayoutTemplate
 
LayoutTemplate is the root Template of the ListView Control. Layout of the control is defined in this template and it is mandatory template in ListView. ListView has no header and footer templates but you can define it in LayoutTemplate. It should contain a placeholder object otherwise you will not be able to display items. LayoutTemplate can also contain DataPager object.
 
ItemTemplate
 
ItemTemplate contains the data bound items that will be displayed for single item. ListView control repeats items for each bound record.
 
GroupTemplate
 
ListView control provide anther template for group layout. GroupTemplate contains the content for group layout. Bound items are partitioned into groups in this GroupTemplate.  Total number of records bound to control is grouped according to defined pattern in GroupTemplate and ItemTemplate is applied to each group. Group size can be defined and a separator can be applied to groups.
 
Bind data to ListView

I am using “stores” table in PUBS Sample database to bind ListView control.  You can bind ListView to SqlDataSource or LinqDataSource or you can bind it programmatically. In both case you have to define templates in ListView control. I will bind ListView control programmatically and also using SqlDataSource.

 

  1. Create new website and write the following HTML code in aspx page of a web form

    <div>
        <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
            <LayoutTemplate>
                <table>
                    <thead>
                        <tr>
                            <th>Store ID</th>
                            <th>Store Name</th>
                            <th>Store Address</th>
                            <th>City</th>
                            <th>State</th>
                            <th>Zip</th>
                        </tr>
                    </thead>
                    <tbody>
                        <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
                    </tbody>
            </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                    <%# Eval("stor_id") %>
                    </td>
                    <td>
                    <%# Eval("stor_name") %>
                    </td>
                    <td>
                    <%# Eval("stor_address") %>
                    </td>
                    <td>
                    <%# Eval("city") %>
                    </td>
                    <td>
                    <%# Eval("state") %>
                    </td>
                    <td>
                    <%# Eval("zip") %>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:ListView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:PUBS %>"
            SelectCommand="SELECT * FROM [stores]"></asp:SqlDataSource>
    </div>
     
     First add a ListView control in aspx page. Add LayoutTemplate in ListView and add <table> tag in it. Then add <thread> tag and add table row and able column header values.  Then add a <tbody> tag and in between <tbody> tag add a Placeholder control. Placeholder control is necessary to display items in ListView control. You also have to write “itemPlaceholder” as id of the Placeholder control otherwise it shows an error. Now add an ItemTemplate and bind items in it. You can also add GroupTemplate before ItemTemplate and define group to display data in ListView control.  Add SqlDataSource in your aspx page and configure it. Now provide DataSourceID in ListView control.
     
  2. Now start debugging and see the ListView control displaying data.

     
  3. You can bind ListView control programmatically as well. Our html code will remain same except SqlDataSource. Remove data source from your aspx page and remove DataSourceID attribute from ListView.
     
  4. Write code below in code behind file of CSharp or Visual Basic

    C#
     
    using System.Data;
    using System.Data.SqlClient;
     
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=local;Initial Catalog=PUBS;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("SELECT * FROM stores", conn);
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            conn.Close();
     
            ListView1.DataSource = ds;
            ListView1.DataBind();
        }
    }
     
    VB.NET
     
    Imports System.Data
    Imports System.Data.SqlClient
    Partial Class _Default
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            Dim conn As New SqlConnection("Data Source=local;Initial Catalog=PUBS;Integrated Security=True")
            Dim cmd As New SqlCommand("SELECT * FROM stores", conn)
            conn.Open()
            Dim da As New SqlDataAdapter(cmd)
            Dim ds As New DataSet()
            da.Fill(ds)
            conn.Close()
     
            ListView1.DataSource = ds
            ListView1.DataBind()
        End Sub
    End Class
     
    Connect to your Server and get data from database. Fill the DataSet with data and bind DataSet to ListVeiw control.
     
  5. Now start debugging again and see the ListView control displaying same data.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Shift focus to next control by pressing Enter key using jQuery in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net