List Box In ASP.NET Web Forms | ASP.NET ListBox | Web Forms Tutorial | ASP.NET
using System;
using System.Collections.Generic;
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)
{
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>");
}
}
}
; }
}
}
Above File is ListBoxAsp\WebForm1.aspx.cs
<%@ 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" BackColor="#66FF66" Font-Bold="True" Font-Italic="True" Font-Size="Large" ForeColor="#CC3399" SelectionMode="Multiple">
<asp:ListItem Value="1">India</asp:ListItem>
<asp:ListItem Value="2">Canada</asp:ListItem>
<asp:ListItem Value="3">Brazil</asp:ListItem>
<asp:ListItem Value="4">Mexico</asp:ListItem>
<asp:ListItem Value="5">Nepal</asp:ListItem>
<asp:ListItem Value="6">Russia</asp:ListItem>
</asp:ListBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
</div>
</form>
</body>
</html>
Comments
Post a Comment