Loop / iterate through ListView items – ASP.NET (Code sample and Video)

1. Create a Web Project.

 

2. Create a class to keep the contents. Here I created Employee class.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace WebAppListViewColor

{

  public class Employees

  {

    public String EmpName { get; set; }

    public String EmpCompetency { get; set; }

    public int EmpScore { get; set; }

  }

}

 

3. Create a container class to hold the list of contents (Employees). I named it Competency class.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace WebAppListViewColor

{

  public class Competency

  {

    List<Employees> lstEmployees = new List<Employees>();

 

    public Competency()

    { //create 3 employees

      Employees emp = new Employees();

      Employees emp1 = new Employees();

      Employees emp2 = new Employees();

 

      emp.EmpName = “Choo Khai”;

      emp.EmpCompetency = “C#”;

      emp.EmpScore = 80;

 

      emp1.EmpName = “Chi Hui”;

      emp1.EmpCompetency = “C#”;

      emp1.EmpScore = 40;

 

      emp2.EmpName = “Safura”;

      emp2.EmpCompetency = “C#”;

      emp2.EmpScore = 50;

      //add 3 employees to list

      lstEmployees.Add(emp);

      lstEmployees.Add(emp1);

      lstEmployees.Add(emp2);

    }

    public List<Employees> GetCompetency()

    {

      return lstEmployees;

    }

  }

}

 

4. Create a ListView and bind to competency class.

 

 

5. Add a Button control to the end of the page.

 

AddButton

 

6. Add the following code to the button click event.

 

    protected void btnLopp_Click(object sender, EventArgs e)

    {

      for (int i = 0; i < ListView1.Items.Count(); i++)

      {

        //Get the label by row

        Label l3 = (Label)ListView1.Items[i].FindControl(“EmpScoreLabel”);

        Label l2 = (Label)ListView1.Items[i].FindControl(“EmpCompetencyLabel”);

        Label l1 = (Label)ListView1.Items[i].FindControl(“EmpNameLabel”);

 

        Response.Write(l1.Text);

        Response.Write(“,”);

        Response.Write(l2.Text);

        Response.Write(“,”);

        Response.Write(l3.Text);

        Response.Write(“<br>”);

      }

    }

 

Download the complete code from http://skydrive.live.com. The sample file name is WebAppListView.rar. My MSN ID is chanmmn@hotmail.com.

Advertisements

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net and tagged . Bookmark the permalink.

8 Responses to Loop / iterate through ListView items – ASP.NET (Code sample and Video)

  1. Pingback: Change cell color in ListView – ASP.NET | Chanmingman's Blog

  2. Pingback: Applying Cascading Style Sheet (CSS) to ListView – ASP.NET (code and video) | Chanmingman's Blog

  3. Pingback: Disable Textbox in EditItemTemplate in ListView – ASP.NET | Chanmingman's Blog

  4. Pingback: Add DropDownList to ListView and bind the DropDownList in ListView to Data Source | Chanmingman's Blog

  5. Visotor says:

    Excellent. After much searching you had the soliton I needed.

  6. db says:

    Thanks much 2 years later, works like a charm in VB.

  7. Pingback: [RESOLVED]Error for cannot operate on variables of type in listview | ASP Questions & Answers

  8. Pingback: [RESOLVED]Cannot convert type 'System.Web.UI.WebControls.ListViewDataItem' to 'System.Web.UI.WebControls.ListItem' | ASP Questions & Answers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s