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

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;
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
        // get from Cache on load se we can use in page
        DropDownListLevels.DataBind();
        SessionLevelsDictionary =
            new Dictionary<int, string>(DropDownListLevels.Items.Count);
 
        foreach (ListItem listItem in 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;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        // get from Cache on load se we can use in page
        DropDownListLevels.DataBind();
        SessionLevelsDictionary =

        new Dictionary<int, string>(DropDownListLevels.Items.Count);
 
        foreach (ListItem listItem in 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.

<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.

About Peter Kellner

Follow me:


Comments

  1. Simply desire to say your article is as amazing. The clearness in your post is simply spectacular and i can assume you’re an expert on this subject. Well with your permission let me to grab your feed to keep updated with forthcoming post. Thanks a million and please continue the rewarding work.

  2. Hello Peter, how can implement this on a
    (my gridview uses a lot ) dropdownlist.
    Could you please give small code explanation? I cant get it right.Rein

Follow

Get every new post delivered to your Inbox

Join other followers: