ASP.NET
  Home arrow ASP.NET arrow Page 2 - ASP.NET Dropdown List Control: Eight Ways ...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

ASP.NET Dropdown List Control: Eight Ways to Bind Data
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 99
    2006-01-04

    Table of Contents:
  • ASP.NET Dropdown List Control: Eight Ways to Bind Data
  • Binding to a Simple Array
  • Binding to a Hashtable
  • Binding to a set of objects

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.  

    More ASP.NET Articles
    More By Jagadish Chaterjee


       · We hope you found this article educational and useful. Please feel free to let the...
       · Hello guys! I am ready for any discussion on this topic. Any doubts, do post them. ...
       · hai, i am new to ajax so please help mei would like to know what is the...
       · hi,i am new here.its really great.this article helped me to explor more.plz tell...
       · thanks for taking time to write this up , i found it really useful and...
       · please give this article in c#.
       · Hi...Really its a good work... God Bless and continue this...
       · Nice article. Please also refer to the article about binding dropdownlist in MVC...
       · should i use direct binding of drop down through data table or should i insert(add)...
     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek