Examining List Controls in ASP.NET

There are many ways to add items to, or populate a List-type ASP.Net Server Control. I’m only addressing the DropDownList here, though the same techniques, for the MOST part, can be used interchangeably with the ListBox, RadioButtonList, and CheckBoxList controls.

The ways we’ll be looking at here are:
In the Web Page itself, Manually (in Page_Load), binding to a Table (field) in a Database, binding to an ArrayList, and adding items after Databinding.

In the Web Page itself
Here’s the way they show you in the Quickstart, so you’re most likely to be familiar with this one:
First Item
Second Item
Third Item

In the Page_Load Event
You can manually populate your DropDownList a couple of different ways (at least that’s all we’re looking at in this tutorial). The first way involves manually adding the items, similar to the above example:
DropDownList.Items.Add(“First Item”)
DropDownList.Items.Add(“Second Item”)
To add items to the DropDownList, along with a Value Field, you can do something like this (the second item is the one that populates the Value:
ddl2.Items.Add(New ListItem(“Item 1″, “1″))
ddl2.Items.Add(New ListItem(“Item 2″, “2″))
Then, of course, there’s dynamic population, with a loop :
Dim x as integer
for x=1 to 10
ddl.items.add(x)
next x
Binding to an ArrayList:
Of course, the other way the Quickstart shows is to add items to an ArrayList, and then binding the DropDownlist to the ArrayList. This particular method provides extra functionality due to the fact that the DropDownlist (or ListBox) does not have a built in ‘Sort’ property. You can sort the ArrayList, and then bind it to the DropDownList. Instead of providing extra code here, we’ll point you to an online example of this:
Arraylist – Adding Items/Sorting/Binding
The example uses a ListBox, but as we said earlier – we’re covering multiple scenarios with this tutorial.
Binding to a Field in a DataBase table:

Continues…

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>