ListBox Example in ASP.NET using C#

ListBox Example in ASP.NET using C#


Posted in : ASP.Net Posted on : November 23, 2010 at 5:49 PM Comments : [ 0 ]

In this article we will introduce with ListBox control in ASP.NET using C#.

ListBox Example in ASP.NET using C#

The ListBox control in ASP.NET is used to display a list of items with scroller. The user can click on items.
A ListBox control provides single or multiple selections of item using SelectionMode property. In this example
we use a ListBox control. It has five items. When the user click on item the corresponding value will be displayed
in label. You can true the Enable AutoPostBack of ListBox control.The following figure shows how can I add item in ListBox.

ListBox.aspx (Design page):

ListBox.aspx (source code):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" 
AutoEventWireup="true" CodeFile="ListBox.aspx.cs" Inherits="ListBox" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<h2 style="color:Green">ListBox in ASP.NET 4 , C#</h2>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" BackColor="#669999" 
Font-Bold="True" Font-Names="Segoe UI" Font-Size="X-Large" Height="105px" 
Width="151px" onselectedindexchanged="ListBox1_SelectedIndexChanged" >
<asp:ListItem>HTML</asp:ListItem>
<asp:ListItem>JAVA</asp:ListItem>
<asp:ListItem>SERVLET</asp:ListItem>
<asp:ListItem>PHP</asp:ListItem>
<asp:ListItem>FLEX 4</asp:ListItem>
</asp:ListBox>
<br />

ListBox.aspx.cs (C# code file):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ListBox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(ListBox1.SelectedIndex == 0)
{
Label1.Text = " This is HTML. ";
}
if (ListBox1.SelectedIndex == 1)
{
Label1.Text = " This is JAVA. ";
}
if (ListBox1.SelectedIndex == 2)
{
Label1.Text = " This is SERVLET. ";
}
if (ListBox1.SelectedIndex == 3)
{
Label1.Text = " This is PHP. ";
}
if (ListBox1.SelectedIndex == 4)
{
Label1.Text = " This is FLEX4. ";
}
}
}

Output:

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics