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:
There are two main sections of code where this takes place. The first is defining the DataSource where the databindinding takes place and the second, which is probably the most overlooked place when first learning ASP.Net is assigning the DataTextField (and/or DataValueField).

In the DataBinding Sub, you would naturally, create a connection to the database. Then, I’d say, in many cases, you would want to only create a DataReader (forward-only) for it’s performance efficiency. Here, you can either add items from one field in the table, or use a value from the table in addtion to the field which provides the ‘visible’ data in the DropDownList.

If you use just one field from the table, after your connection, sql statement an DataReader, you would provide the DataSource similar to this:

DropDownList.DataSource=myDataReader
Then, there are two ways to assign the DataTextField. The first way, would be just after defining the DataSource:
DropDownList.DataTextField=MyDataReader(“FieldName”)
Or, you can do it in the control code itself:

In the same way, if you’ve retrieved two fields in your sql statement, you can assign the DataValueField in these ways also.
After the DropDownList is Populated:
Many times, after the DropDownList has been populated by data, it becomes necessary to add an item to the list. An example of this would be a list in which the first item reads “Select From List”, or something similar. In order to add an item in the first position, the code to do so would be:

DropDownList.items.insert(0,” – Select From List – “)
To merely add an item to the list (at the bottom):
DropDownList.items.add(“Select From List”)
To add an item with a value field (second part of argument):
Dim liItem as ListItem=New ListItem(“ActualItem”, “ItemValue”)
ddl2.Items.Insert(0, liItem)
Selecting Exact Items
To select a particular item in the list, based on the text (visible) item, you can use this code:
DropDownList.SelectedIndex = DropDownList.Items.IndexOf(DropDownList.Items.FindByText(“text”))
To select a particular item in the list, based on the Value of the item, use this code:
DropDownList.SelectedIndex = DropDownList.Items.IndexOf(DropDownList.Items.FindByValue(“value”))
Code for a ListBox is a little simpler:
ListBox.Items.FindByText(“Text”).Selected = true
ListBox.Items.FindByValue(“value”).Selected = true
Hopefully, this tutorial has given you more insight into the DropDownList, and other list-type controls. Be sure to check out our Code Samples Page RadioButtonList, DropDownList, and Listbox code.

Related Posts:

  • No Related Posts
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!