Click here to Skip to main content
11,648,094 members (69,361 online)
Click here to Skip to main content

Complete ListView in ASP.NET 3.5

, 23 Mar 2008 CPOL 921.7K 17.8K 83
Rate this:
Please Sign up or sign in to vote.
Most powerful data binding control which has the power of easier data-binding, flexible pagination, sorting, inserting, deleting, and updating and CSS implement features.


ASP.NET 3.5 introduces a new data binding control named the ListView. ASP.NET already has a lot of data bind controls; it should be more than 10. But the good news is, ListViewcan literally replace all other data binding controls in ASP.NET. ListViewcontrol 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 ListViewstep by step with related code review.

  1. Data binding
  2. Data pager
  3. Sorting
  4. Insert, update and delete


The ListViewis a template driven control which means that it will not render anything. By default, the developer must completely specify the HTML he/she wants to render in the form of templates. To show data in ListViewcontrol, you need to take a LayoutTemplate(to define top level of HTML for output rendering). To show data, you need to take ItemTemplateand AlternativeItemTemplateto show alternative row as with different CSS. Here is the example of simple data binding with AlternativeItemTemplate.

//very simple databinding in ListView
 <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>
  <tr id="itemPlaceholder" runat="server"></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>
  <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>

Here Layouttemplate is making header of the control, and ItemTemplateis showing data taken from table by Bindingcolumns with Labelcontrols, and AlternativeItemTemplatedoes the same as ItemTemplatejust changing CSS for alternative columns.


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

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


It is very easy to sort data in ListView. Sort functionality is defined in the CommandArgumentproperty of the button that contains the columns to be sorted. It occurs when a button with its CommandNameproperty set to Sort is clicked. And you need to make a call to the Sortmethod named onsorting="ListView1_Sorting".

// to allow sort if click on the header of the table make 
//table header controls clickable 
//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>

Insert, Update and Delete

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


    <tr runat="server">
        <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><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>

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, code for Update and Delete will be done using EditItemTemlpate. Please check the attached source files to get the complete code.


  • 24th March 2008: First version of the article


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


About the Author

Ashrafur Rahaman
Software Developer (Senior)
Canada Canada
Software engineer with broad experience in enterprise application development, product deployment automation, software test & test automation, application & system management, and manage big projects and team using proven agile technologies.

Passionate on Microsoft technologies, developed solutions using C#, .net (1.1/2.0/3.5/4), SQL Server (2005/2008). Work on Powershell, SSRS, SSIS, WPF, Ajax, WCF, JQuery.

Develop innovative application with cutting edge technologies always boosting inside.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Member 746380123-Sep-10 20:21
memberMember 746380123-Sep-10 20:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150804.4 | Last Updated 23 Mar 2008
Article Copyright 2008 by Ashrafur Rahaman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid