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>
Next: Dynamic binding to data at run time >>
More ASP.NET Code Articles
More By Jayaram Krishnaswamy