Skip to content

Improved Fast Access to Small Lists On ASPX Pages

Updated: at 06:34 PM

The article below has a minor improvement to the previous article listed on this web site. Specifically, the previous article used the DropDownList as a mechanism to actually retrieve the data for the purposes of storing it in a local Dictionary. This article simply calls the ObjectDataSource directly to retrieve an IEnumerable type and iterates over that. Below is article with the updates. (Original Article is: https://peterkellner.net/2006/08/30/smalllistaccess/)

Most data driven web sites have to manage lots of small lists. Examples include things like states, countries, levels, and even things like sex where you may only have two choices. (especially if you have internationalization issues). The simplest way to store this information is in static lists simply defined programatically. Below is an example of this.

public static string[] MonthNames =
   new string[] { “January”,“February”,“March”,
       “April”,“May”,“June”,“July”,
       “August”,“September”,“October”,“November”,“December”};

This works, but requires you modify your code in order to update a list. Not good in my opinion.

So, the other solution is to store the information in a database table. Many people will say that's overkill, but as long as you Cache the information, performance will not suffer and you gain lots of flexability.

How to Cache Lists in a Database

Cache the List

To cache the list I suggest using an ObjectDataSource with Cache set and then force a load of the ObjectDataSource by calling the Select Method, then using the IEnumerable type, iterate over the ObjectDataSource's assigned type.

<asp:ObjectDataSource ID=”ObjectDataSource1runat=”server
  CacheDuration=”30EnableCaching=”True
  SelectMethod=”GetData
  TypeName=”DataSetLevelsTableAdapters.SessionLevelsTableAdapter>
</asp:ObjectDataSource>

The DataSource TypeName is simply a typed dataset which retrieves a resultset with a primary key and a description.

Use the Cached List on an ASPX Page

To use the cache list on the page, I first store what is in the DropDownList into a Dictionary which I can later reference on the page. Here is what I do in the Page_Load event.

public partial  class Sessions : System.Web.UI.Page
{
   protected Dictionary<int, string> SessionLevelsDictionary;
   protected void Page_Load(object sender, EventArgs e)
   {
      SessionLevelsDictionary =
         new Dictionary<int, string>();
         IEnumerable someData = ObjectDataSource1.Select();
         foreach (StuffData stuffData in someData)
         {
            SessionLevelsDictionary.Add
            (stuffData.Id,stuffData.Name);
         }

Now, I have in the page class a reference to all the values I may want to use on the page. Say, for example I have a Repeater control that is going to display 200 rows of data that includes a foreign key to one of my small list tables. I want to show the data in a Label control but don't want to have any codebehind to set the data. Here is how I can access my dictionary to perform this trick.

<asp:Label ID=”Label4runat=”serverWidth=”90
 Text=’&lt;%# (string) SessionLevelsDictionary[(int) Eval("SessionLevel_id")] %&gt;‘>
</asp:Label>