ASP.NET Dropdown List Control: Eight Ways to Bind Data - Binding to a Simple Array
(Page 2 of 4 )
This is the simplest of all. It is generally used with only one set of values (rather than with two sets of values). Let us go through the following code first:
Private Sub btnSimpleArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpleArray.Click
Me.DropDownList1.Items.Clear()
Dim months() As String = {"January", "February", "March", "April", "May", "June", "July"}
Me.DropDownList1.DataSource = months
Me.DropDownList1.DataBind()
End Sub
The first statement clears all items in the “dropdownlist.” The second statement creates a new array “months()” which can hold any number of strings (assigned during the declaration). It is simultaneously filled with a set of month names. “datasource” is a simple property of “dropdownlist” which can work with several types of “collections”.
At this moment, we assigned a string array “month()” as the data source. The last statement would make the “dropdownlist” fetch all the information from data source “month()” and fill itself. That’s it. We are finished.
I also added two more routines, to check the “text” and “value” of a selected item in a particular “dropdownlist.” Let us go through the two routines available.
Private Sub btnShowText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowText.Click
Me.lblText.Text = Me.DropDownList1.SelectedItem.Text
End Sub
The above routine displays only the “text” (visible to the user) of the selected item within the “dropdownlist.” Let us see the other:
Private Sub btnShowValue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowValue.Click
Me.lblValue.Text = Me.DropDownList1.SelectedItem.Value
End Sub
The above routine displays only the “value” (which is hidden from the user) of the selected item within the “dropdownlist.” In the case of “Binding a Simple Array,” there does not exist two sets of data. It has only one set of data. And thus both the “text” and “value” of the selected item of the “dropdownlist” would be same (which is “text” itself).
Binding to an ArrayList
Working with an “ArrayList” is also quite simple and straightforward. Not only that, it has lot of flexibilities that a simple array does not have. For example, we can remove, clear or do any manipulations within the “ArrayList” (which is a bit tedious to have in a simple array).
Another beauty of an ArrayList is that it can contain any type of “object” (not just strings or integers). We shall examine this feature later.
Now, let us go through the following code:
Private Sub btnArrayList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnArrayList.Click
Me.DropDownList1.Items.Clear()
Dim ar As New ArrayList
ar.Add("January")
ar.Add("February")
ar.Add("March")
ar.Add("April")
ar.Add("May")
ar.Add("June")
ar.Add("July")
Me.DropDownList1.DataSource = ar
Me.DropDownList1.DataBind()
End Sub
I hope it is quite straightforward. You declared a new “ArrayList” and started adding elements to it using the predefined method “add.” But, make sure that even the “ArrayList” can contain only one set of items (and not two sets of items at the moment).
So, even now, the “text” and “value” of any selected item within the “dropdownlist” would still be the same.
Next: Binding to a Hashtable >>
More ASP.NET Articles
More By Jagadish Chaterjee