Improved Fast Access to Small Lists On ASPX Pages

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=”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>
About Peter Kellner

Follow me:


Comments

  1. <asp:DropDownList DataTextField="client_policyType" ID="DropDownListFor_PoicyType" runat="server"
    SelectedValue ='’>
    CA
    VT
    UT
    MD

    <asp:TextBox ID="TextBox1" runat="server"
    Text='’>

    <asp:Label ID="Label1" runat="server" Text='’>

  2. i trying to insert a dropdownlist in a detailsview :

    here is what i deed …

    <asp:DropDownList DataTextField="client_policyType" ID="DropDownListFor_PoicyType" runat="server"
    SelectedValue ='’>
    CA
    VT
    UT
    MD

    <asp:TextBox ID="TextBox1" runat="server"
    Text='’>

    <asp:Label ID="Label1" runat="server" Text='’>

  3. I’m a bit confused by the code.

    How does somedata get populated?
    Does the ObjectDataSource.Select populate it? If so, how does it know which properties to populate from what is retrieved from the db?

    What is the purpose of the GetStuff method as that is just populating from a static set of properties.

  4. have already been reading your blog for a few days. really enjoy your posts. btw i’m doing a study regarding this area. do you happen to know any other good blogs or online forums where I can get more information? many thanks.

  5. I suggest IEnumberable. Here is an example:

    protected void Page_Load(object sender, EventArgs e)
    {
    // this happens everytime. Hopefully, ObjectDataSource will be caching the data for us.
    AddressTypeDictionary = new Dictionary();
    IEnumerable someData = ObjectDataSourceAddressType.Select() as IEnumerable;
    foreach (var stuffData in someData)
    {
    AddressTypeDictionary.Add(stuffData.Id,stuffData.Name);
    }

    }

  6. Administrator says:

    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)]
    public List GetStuff()
    {
    List
    li = new 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

  7. where is StuffData defined?

  8. Luis Santos says:

    … I should clarify that by state I mean country state in this case (NSW) New South Wales, (QLD) Queensland, etc.

    Thank you
    Luis Santos

  9. Luis Santos says:

    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

Follow

Get every new post delivered to your Inbox

Join other followers: