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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 / 82
    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#.
     

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT