tags:

views:

176

answers:

4

I am new to ASP.NET, experienced with WinForms and WPF. Go easy on me.

When my page loads, it hits the database and queries a table for choices to put in a drop down list. Now if you do a postback on the page, do you reload the values in the form_load? It seems unnecessary to hit the database twice. My assumption is that I put the form_load, I hit the database for the enums, then fill the drop down with the values I found.

+1  A: 

These can be loaded at application startup in the Application object. I suppose they won't change too often.

Then used from there. For such 'catalogs' that rarely change (once per year or less) I often hardcode them into an enum type too.

Edit :

For enums such as PaymentStatus or such, which change rarely or never a c# enum mapped to the catalog having the same numeric values, as Randolpho said is ok. For others cache in the HttpRuntimeCache object ("Cache") and put either an absolute expiration either an SqlDependency

Andrei Rinea
doesn't seem very reliable. what if my application runs for a very long time, and enums change slightly more often than that?
MedicineMan
For enums such as PaymentStatus or such, which change rarely or never a c# enum mapped to the catalog having the same numeric values, as Randolpho said is ok. For others cache in the HttpRuntimeCache object ("Cache") and put either an absolute expiration either an SqlDependency
Andrei Rinea
+2  A: 

You control what is called during postback with Page.IsPostBack Property. It returns a boolean indicating when the page is in a postback. Ex:


public partial class _Default : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
        if (!this.IsPostBack) {
            // fill the enums
        }
    }
}

Alfred Myers
+2  A: 

Use the Cache object and store the database results in the cache.

Keltex
+1  A: 

I assume you're talking about a "lookup table", basically something that has a schema of {Id, Name} and is referenced as a foreign key from another table, correct?

This is an age-old question, and there's no "right" answer.

If your enumeration is likely to change at all (perhaps it's controlled by an administrative user), you won't be able to use @Andrei Rinea's suggestion, as you'll need to refresh the value from the database. Ultimately, you'll be best off doing as Keltex suggests and Cache the results with a short expiration -- probably as little as 5 minutes. Just that small amount of caching could increase performance quite a bit, if you're under a heavy load.

If your enumeration is unlikely to change... particularly if it's not modifiable through the UI by an administrator, there's a fun trick that I like to do, which is to map the enumeration to an actual C# enumeration, with the value of the enum tied directly to the primary key of the enumeration table. In this way, you never have to hit the database at all to get a list of possible values.

The drawback to this trick is that a new enumeration item requires a new compilation of your code and a new deployment. This may or may not be ideal, so use the trick with caution.

Randolpho