ASP.NET Dropdown List Control: Eight Ways to Bind Data - Binding to a set of objects
(Page 4 of 4 )
If you have your own structure (defined in the form of “class” or “structure”), you can still use them to create several objects and together bind to the “dropdownlist.”
At this moment, I just created a simple class (CStudent) which can hold a “regdno” and “sname”. I also defined two read/write properties to “set-get” the property values. Let us go through the class first.
Public Class CStudent
Private m_regdno As String
Private m_sname As String
Public Sub New(ByVal r As String, ByVal n As String)
regdno = r
sname = n
End Sub
Public Property regdno() As String
Get
Return m_regdno
End Get
Set(ByVal Value As String)
m_regdno = Value
End Set
End Property
Public Property sname() As String
Get
Return m_sname
End Get
Set(ByVal Value As String)
m_sname = Value
End Set
End Property
End Class
Once you create the above class, we need to declare objects (along with values) to work with those classes. Make sure that a “class” is simply a specification (or user-defined data type). It does not allocate any memory to hold the values (unless you create the objects). The following code creates a few objects based on the class “CStudent” and binds it to the “dropdownlist.”
Private Sub btnObjects_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnObjects.Click
Me.DropDownList1.Items.Clear()
Dim items As New ArrayList
items.Add(New CStudent("1001", "jag1"))
items.Add(New CStudent("1002", "jag2"))
items.Add(New CStudent("1003", "jag3"))
Me.DropDownList1.DataSource = items
Me.DropDownList1.DataTextField = "sname"
Me.DropDownList1.DataValueField = "regdno"
Me.DropDownList1.DataBind()
End Sub
As I mentioned earlier, the “ArrayList” can hold any type of object. And now, you can see how helpful it is!
Binding Manually
Most programmers should already know about this concept. We can manually add different types of items to the “dropdownlist.”
Let us go through the following:
Private Sub btnManual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnManual.Click
Me.DropDownList1.Items.Clear()
Dim li As ListItem
li = New ListItem
li.Text = "first"
li.Value = 1
Me.DropDownList1.Items.Add(li)
li = New ListItem("second")
Me.DropDownList1.Items.Add(li)
li = New ListItem("third", "3")
Me.DropDownList1.Items.Add(li)
For i As Integer = 4 To 10
Me.DropDownList1.Items.Add(New ListItem("item" & i, i))
Next
End Sub
Even within the above program, I tried to give you several ways of adding “one set” or “two sets” of information manually to a “dropdownlist.”
Any comments, suggestions, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |