ASP.NET Code
  Home arrow ASP.NET Code arrow Page 3 - How to Use the ListBox Control in ASP.NET ...
Iron Speed
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 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 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 CODE

How to Use the ListBox Control in ASP.NET 2.0
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 57
    2007-02-27

    Table of Contents:
  • How to Use the ListBox Control in ASP.NET 2.0
  • Usage of the ListBox Control in web pages
  • ListBox Examples
  • Dynamic binding to data at run time
  • Databound ListBox Example
  • Steps in converting a Unbound ListBox to a Bound ListBox

  • 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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    How to Use the ListBox Control in ASP.NET 2.0 - ListBox Examples
    (Page 3 of 6 )

    Simple List

    This example shows filling a ListBox with several items at design time. The next picture shows the ListBox on the design pane filled in with three items. The List Items Collection Editor dialog can be popped up by clicking on an empty space in the items collection property in the List Box properties window shown in the same picture on its right. In this Editor you can begin adding items by providing Key/ Value pairs as shown  for "New Jersey."

    Another way of doing this is to follow the ListBox tasks as shown in the next picture by clicking the smart tags arrow attached to the ListBox. This will be discussed later in the tutorial.

    By using the SelectedIndexChanged event you may be able to write a code so that the Key Value of selected items is displayed in the textbox as shown in the following snippet.

    Partial Class Simplelist
       Inherits System.Web.UI.Page

       Protected Sub ListBox1_SelectedIndexChanged(ByVal sender _
       As Object, ByVal e As System.EventArgs) Handles _
       ListBox1.SelectedIndexChanged
         TextBox1.Text = ListBox1.SelectedValue.ToString 
       End Sub
    End Class

    The source code for this  program (SimpleList.aspx) is shown in the following listing.

    <%@ Page Language="VB" AutoEventWireup="false"
    CodeFile="Simplelist.aspx.vb" Inherits="Simplelist" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
       <title>Simple List</title>
    </head>
    <body>
       <form id="form1" runat="server">
       <div> 
         <asp:ListBox ID="ListBox1" runat="server"
    AutoPostBack="true" >
            <asp:ListItem Value="NJ">New Jersey</asp:ListItem>
            <asp:ListItem Value="C0">Colorado</asp:ListItem>
            <asp:ListItem Value="NY">New York</asp:ListItem>
         </asp:ListBox>
         <asp:TextBox ID="TextBox1" runat="server" Text=''>
         </asp:TextBox></div>
      </form>
    </body>
    </html>

    The result of opening this page in the browser and selecting any of the list items will display its key value in the textbox as shown in the next picture.

    The selected item is bound to a textbox with a Binding expression.

    This example is similar to the previous one, but the Selected Item's value is bound to the textbox with a binding expression as shown in the code used for the "text" property of the textbox. Adding items to the ListBox is similar to the previous example. The ListBox and the textbox are tied up in the form1_load() event as shown in the following snippet for the code behind for this page, BindingExp.aspx.

    Partial Class BindingExp Inherits System.Web.UI.Page Protected Sub form1_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles form1.Load DataBind() End Sub End Class

    The source code for this page (BindingExp.aspx) is shown in the following listing.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="BindingExp.aspx.vb" Inherits="BindingExp" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
       <title>Bound List</title>
    </head>
    <body>
       <form id="form1" runat="server">
       <div>
         <asp:ListBox ID="ListBox1" runat="server"
    AutoPostBack="true">
           <asp:ListItem Value="57">John</asp:ListItem>
           <asp:ListItem Value="35">Tom </asp:ListItem>
           <asp:ListItem Value="22">Lisa</asp:ListItem>
         </asp:ListBox>
         <asp:TextBox ID="TextBox1" runat="server" Text="<%#
    ListBox1.SelectedValue %>">
        </asp:TextBox>
    </div> 
       </form>
    </body>
    </html>

    More ASP.NET Code Articles
    More By Jayaram Krishnaswamy


       · Declarative binding makes it very easy to work with databases even for those who are...
       · I am using the FormView control to create a form with a number of different type of...
       · Does the stored procedure work in the management studio correctly without...
     

    ASP.NET CODE ARTICLES

    - How to Use the ListBox Control in ASP.NET 2.0
    - How to Load XML Documents in ASP.NET 2.0
    - DataGrid Code
    - ASP.NET Guestbook
    - User Controls and Client Side Scripting
    - ASP.NET Programming with Microsoft's AS...
    - ASP.NET Basics (part 3): Hard Choices
    - ASP.NET Basics (part 2): Not My Type
    - ASP.NET Basics (part 1): Nothing But .Net
    - Directory Tree Browser
    - How to get the confirmation of Yes/No from a...
    - Complete example using custom errors and wri...
    - Paging Certain # records per page .NET style
    - General Methods of formatting and Subtractin...
    - .NET LinkButton web control




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway