List Box In ASP.NET Web Forms | ASP.NET ListBox | Web Forms Tutorial | ASP.NET

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ListBoxAsp.WebForm1" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Font-Bold="true" Font-Italic="true" Font-Size="Large" ForeColor="#CC3399">

                <asp:ListItem Value="1" Text="India"></asp:ListItem>

                <asp:ListItem Value="2" Text="SriLanka"></asp:ListItem>

                <asp:ListItem Value="3" Text="USA"></asp:ListItem>

                <asp:ListItem Value="4" Text="South Africa"></asp:ListItem>

                <asp:ListItem Value="5" Text="England"></asp:ListItem>

                <asp:ListItem Value="6" Text="Canada"></asp:ListItem>

            </asp:ListBox>

            <br />

            <br />

            <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"/>

        </div>

    </form>

</body>

</html>

Above File is ListBoxAsp\WebForm1.aspx

















using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (ListBox1.SelectedIndex == -1)
                {
                    Response.Write("Please select any country..");
                }
                else
                {
                    foreach (ListItem li in ListBox1.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>");

                            //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>");
                        }
                    }
                }
            }
            catch (SqlException ex)
            {
                Response.Write("SqlException: " + ex.Message);
            }
            catch (Exception ex)
            {
                Response.Write("Exception: " + ex.Message);
            }
        }
    }
}
Above File is ListBoxAsp\WebForm1.aspx.cs








Comments

Popular posts from this blog

AJAX Method Of jQuery AJAX In ASP.NET Web Forms | Learn ASP.NET Web Forms

Display Auto Increment Value In Text Field After Insert To Database ASP.NET