Click here to Skip to main content
15,867,704 members
Articles / Web Development / ASP.NET

Complete ListView in ASP.NET 3.5

Rate me:
Please Sign up or sign in to vote.
3.53/5 (89 votes)
23 Mar 2008CPOL2 min read 643.3K   20.7K   85   48
Most powerful data binding control which has the power of easier data-binding, flexible pagination, sorting, inserting, deleting, and updating and CSS implement features.
CompleteListView

Introduction

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, 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 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 ListView control, you need to take a LayoutTemplate (to define top level of HTML for output rendering). To show data, you need to take ItemTemplate and AlternativeItemTemplate to show alternative row as with different CSS. Here is the example of simple data binding with AlternativeItemTemplate.

XML
//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 taken from table by Binding columns with Label controls, and AlternativeItemTemplate does the same as ItemTemplate just changing CSS for alternative columns.

DataPager

To add pagination in the listview, you need to add an 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 a simple one.

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

Sorting

It 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 you need to make a call to the Sort method named onsorting="ListView1_Sorting".

HTML
// 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>
 <th></th>
</tr>    

Insert, Update and Delete

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

HTML Code

HTML
<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:

C#
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.

History

  • 24th March 2008: First version of the article

License

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


Written By
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.

Comments and Discussions

 
Questionupdate with access database Pin
Ganesh201520-Mar-15 4:41
Ganesh201520-Mar-15 4:41 
QuestionFew Error on that... Pin
Member 1044769929-Oct-14 9:03
Member 1044769929-Oct-14 9:03 
GeneralI Vote 5 for this Article Pin
DivyaNaidu4866-Oct-14 23:12
DivyaNaidu4866-Oct-14 23:12 
GeneralMy vote of 1 Pin
SoliMoli10-May-14 13:10
SoliMoli10-May-14 13:10 
QuestionThe ID need to click twice to sort Pin
Member 1075306818-Apr-14 7:45
Member 1075306818-Apr-14 7:45 
GeneralMy vote of 1 Pin
UncleJoeIt7-Jan-14 23:14
UncleJoeIt7-Jan-14 23:14 
QuestionAdd a scroll bar? Pin
marc11h13-Dec-13 4:52
marc11h13-Dec-13 4:52 
GeneralMy vote of 1 Pin
JasonMacD15-Oct-13 5:13
JasonMacD15-Oct-13 5:13 
GeneralMy vote of 1 Pin
housemusic13-Aug-13 9:25
housemusic13-Aug-13 9:25 
GeneralMy vote of 2 Pin
RasoulJormand5-Aug-13 4:05
RasoulJormand5-Aug-13 4:05 
GeneralMy vote of 3 Pin
sid_boss2-Aug-13 2:41
sid_boss2-Aug-13 2:41 
QuestionIt's vulnerable to SQL Injection Pin
Pham Dinh Truong26-Jun-13 21:25
professionalPham Dinh Truong26-Jun-13 21:25 
GeneralMy vote of 2 Pin
Sivakuttim1-Mar-13 18:12
Sivakuttim1-Mar-13 18:12 
GeneralMy vote of 5 Pin
Member Kumar Nadar28-Feb-13 20:57
professionalMember Kumar Nadar28-Feb-13 20:57 
GeneralMy vote of 5 Pin
jitendra prajapat31-Jan-13 2:28
jitendra prajapat31-Jan-13 2:28 
QuestionMy Vote Of 4 Pin
Alireza_136228-Jan-13 0:50
Alireza_136228-Jan-13 0:50 
GeneralMy vote of 4 Pin
raj dhakad3-Oct-12 21:12
raj dhakad3-Oct-12 21:12 
QuestionDoubt....? Pin
dineshkanagaraj16-Aug-12 2:20
dineshkanagaraj16-Aug-12 2:20 
GeneralMy vote of 5 Pin
Bilal Fazlani14-May-12 17:18
Bilal Fazlani14-May-12 17:18 
QuestionPLEASE - PLEASE show how to adjust column width in this excellent example!!! Pin
marc11h8-May-12 10:03
marc11h8-May-12 10:03 
GeneralMy vote of 5 Pin
Hitesh Kumar114-Apr-12 2:21
Hitesh Kumar114-Apr-12 2:21 
GeneralMy vote of 5 Pin
Member 1051082210-Feb-12 20:42
professionalMember 1051082210-Feb-12 20:42 
GeneralMy vote of 5 Pin
ankuronlyu18-Oct-11 21:44
ankuronlyu18-Oct-11 21:44 
QuestionYour explication is simple and good! I need it. But I use Web service without database... Pin
aspkiddy4-Oct-11 3:25
aspkiddy4-Oct-11 3:25 
General[My vote of 2] Demo has no database Pin
maximus.dec.meridius2-Jun-11 11:03
maximus.dec.meridius2-Jun-11 11:03 

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

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