ASP. Net ListView DataBinding Tutorial

ASP. Net ListView DataBinding Tutorial,Templates in ListView,Flexibility in rendering,DataBinding is quiet similar to GridView
Share it:

ASP. Net ListView DataBinding Tutorial

For presenting data in list form, we have several options in Asp.Net like ListView, DataList, Repeater, GridView, DropDownList, etc.
Since we have a variety of controls to choose from, the control can be selected based on specific requirements. For e.g. single column with a single selection, the DropDownList is a perfect choice. 
If paging, sorting, the edit is required then GridView will be the right choice. If just need to repeat some HTML code, then Repeater will work out to be better. This article is focused on ListView.





DataBinding is quiet similar to GridView


DataBinding in ListView is almost similar to GridView. There is not much difference. There is a requirement for ItemPlaceHolder. By default the name of that place holder is itemPlaceholder. Asp.Net allows us to custom name (id) also
.

Flexibility in rendering


This ListView is one of the best controls while flexibility is taken into consideration. With the concept of Item Place Holder, we can specify/ configure where the items have to be placed (rotated). In this example, I have made a two-column layout with liquid flow. The number of horizontal cells can be adjusted based on the resolution while resizing the browser.


Templates in ListView


There are a few templates that will ease the development effort using ListView. If you have worked in GridView earlier, then basic templates in ListView are similar to GridView Templates. There are a few special templates. I am highlighting the templates in the following list. These templates can be used to customize rendering.

  • LayoutTemplate
  • GroupTemplate
  • ItemTemplate
  • AlternatingItemTemplate
  • EditItemTemplate
  • InsertItemTemplate
  • ItemSeparatorTemplate
  • GroupSeparatorTemplate
  • EmptyItemTemplate
  • EmptyDataTemplate
  • SelectedItemTemplate


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="WebApplication2.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:ListView runat="server" ID="ListView1">
            <LayoutTemplate>
                <div id="Div1" runat="server" style="width: 100%; border: solid 2px red">
                    <div id="Div2" runat="server" style="float: right; width: 73%; border: solid 2px green">
                        <asp:Panel runat="server" ID="itemPlaceholder">
                        </asp:Panel>
                    </div>
                    <div runat="server" id="Panel1" style="float: left; width: 25%; border: solid 2px blue">
                        My ListView Test
                    </div>
                </div>
            </LayoutTemplate>
            <ItemTemplate>
                <div style="float: left; margin: 2px; width: 24%; border: solid 1px silver; height: 60px;">
                    <asp:Label runat="server" ID="lblName" Font-Bold="true" Text='<%# DataBinder.Eval(Container.DataItem, "ProductName")%>'>
                    </asp:Label>
                    <br /><asp:CheckBox runat="server" ID="chkDiscontinued" Text="Discontinued"
                    Checked='<%# DataBinder.Eval(Container.DataItem, "Discontinued")%>' />
                </div>
            </ItemTemplate>
        </asp:ListView>
        <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="10">
            <Fields>
                <asp:NumericPagerField />
            </Fields>
        </asp:DataPager>
   
    </div>
    </form>
</body>
</html>


Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            BindListView()
        End If
    End Sub
    Private Sub BindListView()
        ListView1.DataSource = GetData(String.Empty)
        ListView1.DataBind()
    End Sub
    Private ReadOnly Property ConnectionString() As String
        Get
            Return "Server=.\SQLEXPRESS;Database=NorthWind;Trusted_Connection=True"
        End Get
    End Property
    Private ReadOnly Property Connection() As SqlConnection
        Get
            Dim ConnectionToFetch As New SqlConnection(ConnectionString)
            ConnectionToFetch.Open()
            Return ConnectionToFetch
        End Get
    End Property
    Public Function GetData(ByVal Expression As String) As DataSet
        Dim SelectQry = "select * from Products"
        Dim SampleSource As New DataSet
        Dim SampleDataAdapter As New SqlDataAdapter(SelectQry, ConnectionString)
        SampleDataAdapter.Fill(SampleSource)
        Return SampleSource
    End Function
    Private Sub ListView1_PagePropertiesChanging(ByVal sender As Object, _
                ByVal e As PagePropertiesChangingEventArgs) Handles ListView1.PagePropertiesChanging
        DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
        BindListView()
    End Sub
Share it:

aspnet

VB.NET

Post A Comment:

0 comments: