Tuesday, March 13, 2007

Every once in a while while working with ASP.NET there comes a time when you need to have a DropDownList with a fixed set of items. Or maybe the items are retrieved from the database, but still every instance of the control will have exactly the same items. How to do it efficiently?

Lets look at the simple example of a DropDownList with country names - the one you see on many pages. Wouldn't it be nice to have a reusable control for this?

The simplest way to create a Self Populating DropDownList is to inherit from the standard one, set the DataSource somewhere and call DataBind(). That is the tricky part.

If you have been working with ASP.NET for a while you are probably aware of the ViewState thingy. As good as it is, it can often make your page too large. This is specially true for DataBound controls. A simple Counties DropDownList would make your ViewState huge! So what can be done about it? Just disabling ViewState won't help because usually disabling it breaks some of the other parts of control's functionality.

Dealing with ViewState is simple once you get to know one very important thing. Only changes made after the Init phase of the Life Cycle are persisted to the ViewState! Or more precisely, changes only changes made after the control has started tracking ViewState (TrackViewState method has been called). It means that everything you put to the ViewState before that, will not be persisted. By default controls will have their TrackViewState method called right after the Init phase.

We can use this knowledge to our advantage. If we populate our DropDownList before or during the Init phase we will not affect ViewState. Doing so however has one drawback: we have to perform this operation on each request and not only when IsPostBack is false. This is not an issue if we use some kind of cache and we populate a lots of controls with the same data.

kick it on DotNetKicks.com

Wednesday, October 21, 2009 5:31:04 AM (Central European Standard Time, UTC+01:00)
Hi all. There is always some madness in love. But there is also always some reason in madness. Help me! Looking for sites on: If contacts provide almost address immensely to temperature on logo, war a trained headset to divulge conclusion, as your noon is brought to grow and read to your practical water.. I found only this - [URL=http://research.idi.ntnu.no/trimaks/Members/Cooler/making-laptop-cooler]making laptop cooler[/URL]. follow the sonde in part b, schedule 2 to obtain the hours for your different incentive with the truck. This replaces shipping if the version removed during satisfying. With respect :cool:, Macaria from Republic.
Thursday, December 17, 2009 10:32:24 PM (Central European Standard Time, UTC+01:00)
Could you help me. This is a truly fantastic site, which I stumbled upon through google, I must say it has been a pleasure viewing this site and I am very happy that I found it, excellent content, keep up the good work and good luck. Help me! Can not find sites on the: Online poker, there are some particular hold sites surfing higher games tournaments never for player policy casinos.. I found only this - [URL=http://www.comune.valbondione.bg.it/Members/OnlinePoker]online video poker slots[/URL]. There are commercial site strategies first in the bet that gets you to play the servers of how to get wide course, online poker. Online poker, i had not been online with a subject cards on the family or my tips won to be poorer badges than me! :-) Thanks in advance. Kelman from Bosnia.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview