Doug
Doug

Reputation: 5318

Loading an ASP.NET DropDownList object

From the examples below showing the loading of a DropDownList in ASP.NET which method is preferred and why?

Method 1:

Build an array of ListItem objects loaded with entity information and use the DropDownList.Items.AddRange method to load the list.

Method 2:

Build a BindableList<T> collection of entity objects and use the DropDownList.DataSource method to load the list.

Method 3:

Build a List<T> collection of entity objects and use the DropDownList.DataSource method to load the list.

Thanks in advance.

Upvotes: 3

Views: 1106

Answers (1)

Shoaib Shaikh
Shoaib Shaikh

Reputation: 4585

All methods results same for dropdown but have their own pros and cons. Here are some short answers:

Method 1 is tightly coupled with List Controls. I don't prefer using method 1 because it don't provide so much flexibility if in future i'll have to bind to data to a grid it will not work.

Speed: This method will be a bit slower if there are too many ListItems. Because you will have to Convert Business Entities to ListItem Object in order to fill dropdown.


Method2 is good option if you choose to work with TwoWay databinding. But it is not supported in asp.net default controls and mechanism, so it will be used in vain with dropdown list.

Speed: This method will be little faster because BindableList implements IEnumerable and it will be iterated once when DataBind is called.


Method 3 is preferably good option because it is generic and extensible and can also work with any bindable object in .net.

Speed: This method will be same as Method2 because List also implements IEnumerable and it will be iterated once when DataBind is called.

Upvotes: 5

Related Questions