Skip to content

Fast Access to Small Lists On ASPX Pages in ASP.NET 2.0

Updated: at 02:12 PM

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 a DropDownList server control to force the retrieval of the list on page load (looking for suggestions on how better to load the list). Below is some typical code I use to do this from an aspx page.

<asp:ObjectDataSource ID=”ObjectDataSource1runat=”serverCacheDuration=”30EnableCaching=”TrueSelectMethod=”GetDataTypeName=”DataSetLevelsTableAdapters.SessionLevelsTableAdapter>
</asp:ObjectDataSource>

<asp:DropDownList ID=”DropDownList1runat=”serverDataSourceID=”ObjectDataSourceLevelsDataTextField=”descriptionDataValueField=”idVisible=”False> </asp:DropDownList>

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;
<span class="kwrd">protected</span> <span class="kwrd">void</span> Page_Load(<span class="kwrd">object</span> sender, EventArgs e)
{

    <span class="rem">// get from Cache on load se we can use in page</span>
    DropDownListLevels.DataBind();
    SessionLevelsDictionary =
        <span class="kwrd">new</span> Dictionary&lt;<span class="kwrd">int</span>, <span class="kwrd">string</span>&gt;(DropDownListLevels.Items.Count);

    <span class="kwrd">foreach</span> (ListItem listItem <span class="kwrd">in</span> DropDownListLevels.Items)
    {
        SessionLevelsDictionary.Add
            (Convert.ToInt32(listItem.Value), listItem.Text);
    }

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.

public partial class  Sessions : System.Web.UI.Page
{
    protected Dictionary<int, string> SessionLevelsDictionary;
<span class="kwrd">protected</span> <span class="kwrd">void</span> Page_Load(<span class="kwrd">object</span> sender, EventArgs e)
{
    <span class="rem">// get from Cache on load se we can use in page</span>
    DropDownListLevels.DataBind();
    SessionLevelsDictionary =

    <span class="kwrd">new</span> Dictionary&lt;<span class="kwrd">int</span>, <span class="kwrd">string</span>&gt;(DropDownListLevels.Items.Count);

    <span class="kwrd">foreach</span> (ListItem listItem <span class="kwrd">in</span> DropDownListLevels.Items)
    {
        SessionLevelsDictionary.Add
            (Convert.ToInt32(listItem.Value), listItem.Text);
    }</pre>

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=’&lt;%# (string) SessionLevelsDictionary[(int) Eval("SessionLevel_id")] %&gt;‘>
</asp:Label>

I'm still looking for a better way to do this. If anyone has any ideas, please add them in the comments below.

Check out the ORM (Object Relational Mapper) PRISMA. The database access method I use in all my projects