ASP.NET
  Home arrow ASP.NET arrow Page 4 - 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 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.

       · 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 6 hosted by Hostway
    Stay green...Green IT