asp.net c# tutorials and examples code for beginners

asp.net c# ListBox example

ListBox Example
ListBox is a Standard ToolBox Control. Here I show simple examples of ListBox Control.

ListBox
First create a Web Form name ListBox.aspx. Then place a ListBox control and two Label control. For ListBox data I use static ListItem. When someone select an Item then a Label control show the selected Item. The source code of ListBox.aspx is below.
<%@ Page Language="C#" %>

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

<script runat="server">
    protected void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e) {
        Label1.Text = "Your Favorite Control is: " +
                    ListBox1.SelectedItem.Text.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ListBox Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Font-Size="Medium" Font-Bold="true" ForeColor="CornflowerBlue" />
        <br /><br />
        <asp:Label ID="Label2" runat="server" Text="Favorite Control?"></asp:Label>
        <br />
        <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
            <asp:ListItem>AdRotator</asp:ListItem>
            <asp:ListItem>BulletedList</asp:ListItem>
            <asp:ListItem>CheckBox</asp:ListItem>
            <asp:ListItem>ListBox</asp:ListItem>
            <asp:ListItem>CheckBoxList</asp:ListItem>
            <asp:ListItem>RadioButton</asp:ListItem>
            <asp:ListItem>RadioButtonList</asp:ListItem>
            <asp:ListItem>DropDownList</asp:ListItem>
            <asp:ListItem>MultiView</asp:ListItem>
            <asp:ListItem>Wizard</asp:ListItem>
            <asp:ListItem>Xml</asp:ListItem>
        </asp:ListBox>
    </div>
    </form>
</body>
</html>
SelectionMode Multiple
Here I show another example of ListBox Control. Where you see the ListBox’s property SelectionMode set Multiple. First create a Web Form name ListBoxSelectionModeMultiple.aspx. Then add a listBox Control, two Label Control and a Button Control. Set the SelectionMode property value Multiple in ListBox Control. When someone clicks the Button then he can see the selected Items in Label. Here is the source code.
<%@ Page Language="C#" %>

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

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e) {
        Label1.Text = "Your favorite Control(s): <br />";
        foreach(ListItem li in ListBox1.Items){
            if (li.Selected == true) {
                Label1.Text += li.Text.ToString() + "<br />";
            }
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>ListBox Example: SelectionMode Multiple</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Font-Size="Medium" Font-Bold="true" ForeColor="Crimson" />
        <br /><br />
        <asp:Label ID="Label2" runat="server" Text="Favorite Control?"></asp:Label>
        <br />
        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
            <asp:ListItem>AppearanceEditorPart</asp:ListItem>
            <asp:ListItem>BehaviorEditorPart</asp:ListItem>
            <asp:ListItem>CatalogZone</asp:ListItem>
            <asp:ListItem>ConnectionsZone</asp:ListItem>
            <asp:ListItem>DeclarativeCatalogPart</asp:ListItem>
            <asp:ListItem>EditorZone</asp:ListItem>
            <asp:ListItem>ImportCatalogPart</asp:ListItem>
            <asp:ListItem>LayoutEditorPart</asp:ListItem>
            <asp:ListItem>PageCatalogPart</asp:ListItem>
            <asp:ListItem>PropertyGridEditorPart</asp:ListItem>
            <asp:ListItem>ProxyWebPartManager</asp:ListItem>
            <asp:ListItem>WebPartManager</asp:ListItem>
            <asp:ListItem>WebPartZone</asp:ListItem>
        </asp:ListBox>
        <br /><br />
        <asp:Button ID="Button1" runat="server" Text="Show Selection" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>
Related asp.net example