Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 837
Doug Avatar asked Dec 17 '25 01:12

Doug


1 Answers

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.

like image 92
Shoaib Shaikh Avatar answered Dec 19 '25 16:12

Shoaib Shaikh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!