Sponsored Ad

Monday, May 3, 2010

How to work With ListBox in C# | Add Items | Get Selected Items

This sample program will help you to work with listbox using C#. This small demonstration will help you add items in listbox and get selected values from listbox.

There is two separate button for different functionalities.

 

OutPut:

 

How to work With ListBox in C# | Add Items | Get Selected Items

 

<%@ 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 btnClick_Click(object sender, EventArgs e)
    {
        lblOutput.Text = "Your selection is: <br>";
        foreach (ListItem lst in lstSites.Items)
        {
            if (lst.Selected == true)
            {
                lblOutput.Text += lst.Text + "<br>";
            }
       }
    }

    protected void btnAddItem_Click(object sender, EventArgs e)
    {
        lstSites.Items.Add(txtInput.Text.ToString());
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ListBox ID="lstSites" runat="server" SelectionMode="multiple">
                <asp:ListItem>IndiHub.com</asp:ListItem>
                <asp:ListItem>BharatClick.com</asp:ListItem>
                <asp:ListItem>SharepointBank.com</asp:ListItem>
                <asp:ListItem>SoftwareTestingNet.com</asp:ListItem>
            </asp:ListBox>
            <br />
            <br />
            <asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:Label ID="lblOutput" runat="server" Text="Label"></asp:Label>
            <br />
            <br />
            <asp:Button ID="btnClick" runat="server" Text="Get Selected Items" OnClick="btnClick_Click" />
            <asp:Button ID="btnAddItem" runat="server" Text="Add Items" OnClick="btnAddItem_Click" />
        </div>
    </form>
</body>
</html>

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers