Saturday, June 14, 2014

ListBox Control

Properties
1. SelectionMode:-By default selection mode is single. Which means user can select one list item. When try to select other item it clear the selection of the other item.
2. To select multiple items:- properties--> SelectionMode=multiple
Example :-
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_25_ListBox.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <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">  
       <asp:ListItem Value="1">Diploma</asp:ListItem>  
       <asp:ListItem Value="2">Graduate</asp:ListItem>  
       <asp:ListItem Value="3">Post Graduate</asp:ListItem>  
       <asp:ListItem Value="4">Doctrate</asp:ListItem>  
     </asp:ListBox>  
     <br />  
     <br />  
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace _25_ListBox  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       foreach (ListItem li in ListBox1.Items)  
       {  
         if (li.Selected)  
         {  
           Response.Write("Text = " + li.Text + ", ");  
           Response.Write("Value = " + li.Value + ", ");  
           Response.Write("Index = " + ListBox1.Items.IndexOf(li).ToString());  
           Response.Write("<br/>");  
         }  
       }  
     }  
   }  
 }  

No comments:

Post a Comment