VB .Net ListView


This article is written by Pon Saravanan  on 29-Nov-09 Last modified on :06-Jun-10





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 single selection the DropDownList is the perfect choice. If paging, sorting, 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 of 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 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 few special templates. I am highlighting the templates in the following list. These templates can be used customize rendering.

LayoutTemplate
GroupTemplate
ItemTemplate
AlternatingItemTemplate
EditItemTemplate
InsertItemTemplate
ItemSeparatorTemplate
GroupSeparatorTemplate
EmptyItemTemplate
EmptyDataTemplate
SelectedItemTemplate

Source Code

Markup (*.ASPX)

<%@ Page Language="vb" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListView runat="server" ID="ListView1">
            <LayoutTemplate>
                <div runat="server" style="width: 100%; border: solid 2px red">
                    <div 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>

Code-Behind (*.VB)

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
End Class



« Previous - ASP.Net Repeater
- Next »







Comments
  • GUEST
    can you teach me about this? 8/25/2010 8:43:51 PM

  • GUEST
    Please let me know what do you wish to learn from this aarticle and sample source code? 8/25/2010 11:38:25 PM


Comments
   
Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)    



Spam Bot Trap



   



Select Theme
White
Gray
Blue
Brown
GraySimple