HOW TO: Display runtime-generated data in a ListView control in SharePoint web part

This blog post is a contribution from Bharat Rathod, an engineer with the SharePoint Developer Support team.

We often find that ListView control is used to display data which is fetched from a database directly.  After reading a lot of articles where I could not find the specific article that talks about using a generic class to build an array of data at runtime, I decided to write this one.

I have created a Visual web part for my SharePoint 2010 web part.  Add a ListView control to the ASCX file of the user control present in the Visual Web part.  In this example, I am trying to display an array of objects of type string.  In my case, it’s called URL.  After adding the ListView control, set the necessary properties like <LayoutTemplate/> and <ItemTemplate/> as shown below.

 <asp:ListView runat="server" ID="ListView1" EnableModelValidation="True">
     <table runat="server" id="table1" >
       <tr runat="server" id="itemPlaceholder" ></tr>
     <tr id="Tr1" runat="server">
       <td id="Td1" runat="server">
         <%-- Data-bound content. --%>
         <asp:Label ID="NameLabel" runat="server" 
           Text='<%#Eval("Url") %>' />                   
 <asp:GridView ID="GridView1" runat="server">

In the Page_Load method of my user control, I initialize an ObjectDataSource object and set its ID, SelectMethod and TypeName properties.

NOTE: A very common mistake that most of us do is setting the TypeName to the user control’s type and not of the generic class i.e., in my case, class with the name siteCollection.

Now, when the ListView1.DataBind() is called, the select method specified “GetSiteCollectionUrl” is called and the data is built at runtime.  Here’s the code for the user control’s class file.

 using System;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Data;
 using System.Collections.Generic;
 using System.Collections;
 namespace ListViewProject.VisualWebPart1
     public partial class VisualWebPart1UserControl : UserControl
         protected void Page_Load(object sender, EventArgs e)
             ObjectDataSource obj = new ObjectDataSource();
             obj.ID= "ObjectDataSource1";
             obj.SelectMethod= "GetSiteCollectionUrl";
             obj.TypeName = (typeof(siteCollection)).AssemblyQualifiedName;
             ListView1.DataSource = obj;
     public class siteCollection
         public siteCollection() {}
         public static List<SiteCollectionInfo> GetSiteCollectionUrl()
             List<SiteCollectionInfo> oUrl = new List<SiteCollectionInfo>();
             SiteCollectionInfo col1 = new SiteCollectionInfo();
             col1.Url= "";
             SiteCollectionInfo col2 = new SiteCollectionInfo();
             col2.Url= "";
             return oUrl;

And here’s the code for the SiteCollectionInfo type.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 namespace ListViewProject.VisualWebPart1
     public class SiteCollectionInfo
         public SiteCollectionInfo() { }
         public string Url { get; set; }

Hope this post was helpful!