5,276,156 members and growing! (20,140 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Intermediate License: The Code Project Open License (CPOL)

Complete ListView in ASP.NET 3.5

By Ashrafur Rahaman

Most powerful data binding control which has the power of easier data-binding, flexible pagination, sorting, inserting, deleting, and updating and CSS implement features.
C# (C# 3.0, C#), .NET (.NET, .NET 3.5), Visual Studio (VS2008, VS), Dev

Posted: 23 Mar 2008
Updated: 23 Mar 2008
Views: 7,309
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 1.83 Rating: 2.17 out of 5
2 votes, 33.3%
1
1 vote, 16.7%
2
1 vote, 16.7%
3
0 votes, 0.0%
4
2 votes, 33.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
CompleteListView

Introduction

ASP.NET 3.5 introduces a new data binding control named the ListView. ASP.NET already has a lot data bind control; it should be more than 10. But the good news is, ListView can literally replace all other data binding controls in ASP.NET. ListView control makes data binding easier than previous controls. It has included styling with CSS, flexible pagination, and sorting, inserting, deleting, and updating features.

Complete ListView

Listview having full data control support

In this article I will describe features in ListView step by step with related code review

  1. Data binding
  2. Data Pager
  3. Sorting
  4. Insert, Update and Delete

DataBinding

The ListView is template driven control which means that it will not render anything by default developer must completely specify the HTML he/she want to render in the form of templates. To show data in ListView control need to take a LayoutTemplate (to define top level of HTML for output rendering).To show data need to take ItemTemplate and AlternativeItemTemplate can be taking to show alternative row as with different CSS. Here is the example of Simple data binding with AlternativeItemTemplate.

   //very simple databinding in ListView
   <LayoutTemplate>
    <table border="0" cellpadding="1">
     <tr style="background-color:#E5E5FE">
      <th align="left"><asp:LinkButton ID="lnkId" runat="server">Id</asp:LinkButton></th>
      <th align="left"><asp:LinkButton ID="lnkName" runat="server">Name</asp:LinkButton></th>
      <th align="left"><asp:LinkButton ID="lnkType" runat="server">Type</asp:LinkButton></th>
      <th></th>
     </tr>
     <tr id="itemPlaceholder" runat="server"></tr>
    </table>
   </LayoutTemplate>
   <ItemTemplate>
     <tr>
      <td><asp:Label runat="server" ID="lblId"><%#Eval("ID") %></asp:Label></td>
      <td><asp:Label runat="server" ID="lblName"><%#Eval("FirstName")+" "+Eval("LastName") %></asp:Label></td>
      <td><asp:Label runat="server" ID="lblType"><%#Eval("ContactType") %></asp:Label></td>
      <td></td>
     </tr>
   </ItemTemplate>
   <AlternatingItemTemplate>
     <tr style="background-color:#EFEFEF">
      <td><asp:Label runat="server" ID="lblId"><%#Eval("ID") %></asp:Label></td>
      <td><asp:Label runat="server" ID="lblName"><%#Eval("FirstName")+" "+Eval("LastName") %></asp:Label></td>
      <td><asp:Label runat="server" ID="lblType"><%#Eval("ContactType") %></asp:Label></td>
      <td></td>
     </tr>
   </AlternatingItemTemplate>

Here Layout template is making header of the control, and ItemTemplate is showing data taking from table by Binding columns with Label controls, and AlternativeItemTemplate doing same as ItemTemplate just chaning css for alternative columns.

DataPager

To add pagination in the listview need to add a asp:DataPager control, better to put this control inside LayoutTemplate at the end of the LayoutTemplate. DataPager control has many options to show pagination and these are useful too. Here I have just used simple one.

      <asp:DataPager ID="ItemDataPager" runat="server" PageSize="5">
          <Fields>
               <asp:NumericPagerField ButtonCount="2" />
           </Fields>
      </asp:DataPager>    

Sorting

This is very easy to sort data in ListView. Sort functionality is defined in the CommandArgument property of the button that contains the columns to be sorted. It occurs when a button with its CommandName property set to Sort is clicked. And need to make a call for Sort method named onsorting="ListView1_Sorting".

   // to allow sort if click on the header of the table make table header controls clikable 
   //and give commandname and commandargument
   <tr style="background-color:#E5E5FE">
    <th align="left"><asp:LinkButton ID="lnkId" runat="server" CommandName="Sort" CommandArgument="ID">Id</asp:LinkButton></th>
    <th align="left"><asp:LinkButton ID="lnkName" runat="server" CommandName="Sort" CommandArgument="FirstName">Name</asp:LinkButton></th>
    <th align="left"><asp:LinkButton ID="lnkType" runat="server" CommandName="Sort" CommandArgument="ContactType">Type</asp:LinkButton></th>
    <th></th>
   </tr>        

Insert, Update and Delete

To insert data into ListView need to add a tag in ListView named InsertItemTemplate. Add to add inserted code add code in ItemCommand.

HTML code

   <InsertItemTemplate>
       <tr runat="server">
           <td></td>
           <td>
           <asp:TextBox ID="txtFname" runat="server" Text='<%#Eval("FirstName") %>' Width="100px">First Name</asp:TextBox>
           <asp:TextBox ID="txtLname" runat="server" Text='<%#Eval("LastName") %>' Width="100px">Last Name</asp:TextBox>
           </td>
           <td><asp:TextBox ID="txtCtype" runat="server" Text='<%#Eval("ContactType") %>' Width="100px">Contact Type</asp:TextBox></td>
           <td><asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" /></td>
       </tr>
   </InsertItemTemplate>
    

CS code (in the cs file insert this code in ItemCommand)

        if (e.CommandName == "Insert")
        {
            TextBox txtFname = (TextBox)e.Item.FindControl("txtFname");
            TextBox txtLname = (TextBox)e.Item.FindControl("txtLname");
            TextBox txtCtype = (TextBox)e.Item.FindControl("txtCtype");
            string insertCommand = "Insert into [Contacts] ([FirstName],[LastName],[ContactType]) Values('" + txtFname.Text + "', '" 
                 + txtLname.Text + "', '" + txtCtype.Text + "');";
            SqlDataSource1.InsertCommand = insertCommand;
        }

In the same way codes for Update and Delete will be done using EditItemTemlpate. Please check attached codes to get complete code.

History

This is the First version of that article -24th March 2008

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ashrafur Rahaman


Lead programmers on project to finish the outsourced product, analyze requirement and define solution architecture for project. Design and maintain database. Software quality assurance, source control, bug tracking, feature and project schedule planning. Developed Statement of Work, setup project plan, designed Data Model and Web Application architecture in .NET 2.0/3.5 and SQL Server, and supervise a .net developer’s team.
Occupation: Team Leader
Company: Latitude-23
Location: Bangladesh Bangladesh

Other popular ASP.NET Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
Subject  Author Date 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Mar 2008
Editor:
Copyright 2008 by Ashrafur Rahaman
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project