Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Caching dropdown

Status
Not open for further replies.

manjulam

Programmer
Feb 27, 2002
103
US
I have two dropdowns that get populated from the database on page load. But once it is populated, the values dont change. But everytime the page reloads, it will still fetch from the db isnt it? Is there anyway of caching these controls so that i can minimise the DB fetches? Any thoughts please....
 
The easiest step is to only bind the first time the page loads. You'd do this like:

Code:
//Page_Load

if(!IsPostBack)
{
   //get data
   //bind control
}

ViewState will keep track of the values and load them automatically so you don't have to keep re-querying.

A second thing you can do (assuming the data is always the same) is to cache the results of the query.

//Page_Load

if(!IsPostBack)
{
DataTable data;

if( Cache["MyData"] != null )
data = (DataTable)Cache["MyData"];
else
{
data = //fetch data
Cache["MyData"] = data; //sliding expiration would
//be nice
}

//bind control using "data"
}
[/code]

A third thing you can do is can do is cache the whole DropDownList to save on expensive data binding.

Finally, you can cache the whole page if you really want with OutputCaching:


It all depends on your needs and preferences.
 
I am using the first two methods now.
How do you cache the whole dropdownlist?
 
ASP.NET server-side caching can cache anything. It's pretty sweet. You'd do something like this:

Code:
ddlMyDropDown = Cache["MyDropDown"] as DropDownList;

if( ddlMyDropDown == null )
{
    //bind dropdown
    Cache["MyDropDown"] = ddlMyDropDown;
}
 
No problem. Something I forgot to mention, however, is that all pages retrieving items from the cache use the exact same item so you may just want to change my retrieval code to obtain a COPY of the cached item for each page.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top