ARTICLE

Bind Records with ListView control in VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET July 25, 2011
Here we will see how to bind records with ListView control in VB.NET.
 
Reader Level:

ListView control makes data binding easier than previous controls. It has included styling with CSS, flexible pagination, and sorting, inserting, deleting, and updating features. In this article we create a SQL server database table with ID, title, description and author name. Drag and drop a ListView control on the asp.net form to bind records with database.

Templates available with ListView.

1. LayoutTemplate.

2. ItemTemplate.

3. AlternatingItemTemplate


LayoutTemplate

LayoutTemplate is used to show the overall layout of the template. We can give the title for our page. We need to place a placeholder the id of this control specifically should be "itemPlaceholder".

ItemTemplate

ItemTemplate is visual layout of the each item that we iterated over the each element inside the ListView Control. Specify the individual items that you want to display in the ListView.

AlternatingItemTemplate - Defines how alternating items are displayed.

Creating a Table

Create a table in database named as Articletable. Table looks like this.

CREATE TABLE [dbo].[Articletable](

      [ID] [int] NOT NULL,

      [Title] [varchar](200) NULL,

      [Description] [varchar](400) NULL,

      [Author] [varchar](50) NULL

)

Inserting data in the articletable.

INSERT INTO Articletable VALUES(1,'How to validate dropdownlist in asp.net','Here, we will learn how to validate a DropDownList in ASP.NET.','Rohatash Kumar');

GO

INSERT INTO Articletable VALUES(2,'Introduction to .NET Assemblies in VB.NET',' Here is a comprehensive introduction to .NET assemblies.','sunil Kumar');

go

INSERT INTO Articletable VALUES(3,'BinaryReader and BinaryWriter classes in VB.NET','In this article I will explain about BinaryReader and BinaryWriter Classes in VB.NET.','Deepak Kumar');

 

go

INSERT INTO Articletable VALUES(4,'StreamWriter class in VB.NET','This article shows how to create a new text file and write a string to it.','Rohatash Kumar');

go

select * from articletable;

OUTPUT

tab1.jpg

Now drag and drop a ListView control from the Toolbox on the Form. The form looks like this.

ListView control

ListView1

The ASP. NET code for the ListView.aspx page.

<!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 ID="ListView1" runat="server" DataSourceID="SqlDataSource1">                     

<ItemTemplate>

 

    <tr style="background-color:#DCDCDC;color: #000000;">

        <td>

            <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />

        </td>

        <td>

            <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />

        </td>

        <td>

            <asp:Label ID="DescriptionLabel" runat="server"

                Text='<%# Eval("Description") %>' />

        </td>

        <td>

            <asp:Label ID="AuthorLabel" runat="server" Text='<%# Eval("Author") %>' />

        </td>

    </tr>

</ItemTemplate>

              <LayoutTemplate>

              <table runat="server">

                  <tr runat="server">

                      <td runat="server">

                          <table ID="itemPlaceholderContainer" runat="server" border="1"

                              style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">

                              <tr runat="server" style="background-color:#DCDCDC;color: #000000;">

                                  <th runat="server">

                                      ID</th>

                                  <th runat="server">

                                      Title</th>

                                  <th runat="server">

                                      Description</th>

                                  <th runat="server">

                                      Author</th>

                              </tr>

                              <tr ID="itemPlaceholder" runat="server">

                              </tr>

                          </table>

                      </td>

                  </tr>

                  <tr runat="server">

                      <td runat="server"

                          style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;">

                      </td>

                  </tr>

              </table>

          </LayoutTemplate>        

</asp:ListView>

       <asp:SqlDataSource ID="SqlDataSource1" runat="server"

ConnectionString="<%$ ConnectionStrings:userinfoConnectionString2 %>"

SelectCommand="SELECT * FROM [Articletable]"></asp:SqlDataSource>

 </div>

    </form>

</body>

</html>

 

Now run the application and test it.


ListView with database data

 

ListView2

 

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor