Improved Fast Access to Small Lists On ASPX Pages
Using ASP.NET 2.0
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: http://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=”ObjectDataSource1″ runat=”server”
CacheDuration=”30″ EnableCaching=”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.
21 public partial class Sessions : System.Web.UI.Page
22 {
23 protected Dictionary<int, string> SessionLevelsDictionary;
24
25 protected void Page_Load(object sender, EventArgs e)
26 {
27 SessionLevelsDictionary =
28 new Dictionary<int, string>();
29
30 IEnumerable someData = ObjectDataSource1.Select();
31 foreach (StuffData stuffData in someData)
32 {
33 SessionLevelsDictionary.Add
34 (stuffData.Id,stuffData.Name);
35 }
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=”Label4″ runat=”server” Width=”90″
Text=’<%# (string) SessionLevelsDictionary[(int) Eval(”SessionLevel_id”)] %>‘>
</asp:Label>











February 2nd, 2007 at 4:11 am
Hello Peter,
Luis (newbie) here.
I’m trying to follow your explanation on how to store and load a Dictionary into a web control. In my case I want to populate a drop down template field to maintain the state. When the update page is loaded the drop down is positioned according to the value in the database and user can select another state which will then populate the “updated” state code back to the table… In your example I just don’t understand what is / where StuffData is coming from.
It may all be due to the fact that I cannot find information on what is or how to use IEnumerable
Your feedback will be much appreciated.
Luis
February 2nd, 2007 at 4:25 am
… I should clarify that by state I mean country state in this case (NSW) New South Wales, (QLD) Queensland, etc.
Thank you
Luis Santos
September 27th, 2007 at 11:45 pm
where is StuffData defined?
September 28th, 2007 at 6:58 am
Here is the StuffData Class:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Collections.Generic;
///
/// Summary description for Stuff
///
///
[DataObject]
public class Stuff
{
public Stuff()
{
}
[DataObjectMethod(DataObjectMethodType.Select,true)] GetStuff() li = new List ();
public List
{
List
li.Add(new StuffData(1,”peter”,”3443 Yuba Ave”));
li.Add(new StuffData(1,”tammy”,”3443 Yuba Ave”));
li.Add(new StuffData(1,”roger”,”3443 Yuba Ave”));
return li;
}
}
public class StuffData
{
public StuffData(int id, string name, string address)
{
this.id = id;
this.name = name;
this.address = address;
}
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string address;
public string Address
{
get { return address; }
set { address = value; }
}
}
-Peter Kellner