Follow us

Header Ads

ListBox Control in Asp.Net WebForm

 ListBoxControl.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="1ListBoxControl.aspx.cs" Inherits="ListBoxControl" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h1>ListBox Control1</h1>
            <br />

            <asp:ListBox ID="ListBox1" runat="server" Rows="5" SelectionMode="Multiple">
                <asp:ListItem Value="1">Pakistan</asp:ListItem>
                <asp:ListItem Value="2">India</asp:ListItem>
                <asp:ListItem Value="3">Siralanka</asp:ListItem>
                <asp:ListItem Value="4">Bangaladesh</asp:ListItem>
                <asp:ListItem Value="5">Canada</asp:ListItem>
                <asp:ListItem Value="6">Afghanistan</asp:ListItem>
            </asp:ListBox>
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
             <h1>ListBox Control2</h1>
            <br />
            <!-- Enable and Disable is use to Enable and Disable ListBox  -->

            <asp:ListBox ID="ListBox2" runat="server" Rows="5" SelectionMode="Multiple" BackColor="#99FF33" Font-Bold="True" Font-Italic="True">
                <asp:ListItem Value="1">Pakistan</asp:ListItem>
                <asp:ListItem Value="2">India</asp:ListItem>
                <asp:ListItem Value="3">Siralanka</asp:ListItem>
                <asp:ListItem Value="4">Bangaladesh</asp:ListItem>
                <asp:ListItem Value="5">Canada</asp:ListItem>
                <asp:ListItem Value="6">Afghanistan</asp:ListItem>
            </asp:ListBox>
            <br />
            <br />
            <asp:Button ID="Button2" runat="server" Text="Submit" OnClick="Button2_Click" />
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ListBoxControl : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if(ListBox1.SelectedIndex == -1)
        {
            Response.Write("Please select any country");
        }
        else
        {
            Response.Write("Selected Item Text is:" + ListBox1.SelectedItem.Text + "<br>");
            Response.Write("Selected Item Value is:" + ListBox1.SelectedItem.Value + "<br>");
            Response.Write("Selected Item Index is:" + ListBox1.SelectedIndex + "<br>");
        }
        
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (ListBox2.SelectedIndex == -1)
        {
            Response.Write("Please select any country");
        }
        else
        { 
            // li is object of list item
            foreach(ListItem li in ListBox2.Items)
            {
                if(li.Selected)
                {
                    Response.Write("Selected Item Text is:" + li.Text + "<br>");
                    Response.Write("Selected Item Value is:" + li.Value + "<br>");
                    Response.Write("Selected Item Index is:" + ListBox1.Items.IndexOf(li)+ "<br>");
                    Response.Write("------------------------------------------------<br>");
                }
               
            }
            
        }
    }
}
        </div>
    </form>
</body>
</html>

 ListBoxControl.aspx.cs



Post a Comment

0 Comments