How to Show Listbox Item with serial numbers and separated with Comma(,) in

How to Show Listbox Item with serial numbers in

Hi Friends,
How to get serial numbers on Selected ListBox items and Show / display in below Label. The below code will help you to get the serial numbers selected items in a list box,

Create Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListBox-with-Comma.aspx.cs" Inherits="Example_ListBox_with_Comma" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" 
            <asp:ListItem>North America</asp:ListItem>
            <asp:ListItem>South America</asp:ListItem>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add" 
            Width="84px" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>

C# : Code

 protected void Button1_Click(object sender, EventArgs e)
        int j=1;
        if (ListBox1.SelectedItem.Text == "Select")
            Label1.Text = "No Items Selected...";
            string get_soft_skills = "";
            for (int i = 0; i < ListBox1.Items.Count; i++)
                if (ListBox1.Items[i].Selected)
                    get_soft_skills += j.ToString() + ". " + ListBox1.Items[i].Value + ",<br/> ";
                    Label1.Text = get_soft_skills;


Post a Comment