Saturday, June 14, 2014

Checkboxlist and Listbox Real Time example

Example :-
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_26.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:CheckBoxList ID="CheckBoxList1" runat="server"   
       RepeatDirection="Horizontal" AutoPostBack="True"   
       onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">  
       <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:CheckBoxList>  
     <br />  
     <asp:ListBox ID="ListBox1" runat="server" Height="92px"   
       SelectionMode="Multiple" Width="110px"></asp:ListBox>  
     <br />  
     <br />  
     <asp:Label ID="Label1" runat="server"></asp:Label>  
   </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 _26  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
     }  
     protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)  
     {  
       ListBox1.Items.Clear();  
       foreach (ListItem li in CheckBoxList1.Items)  
       {  
         if (li.Selected)  
         {  
           ListBox1.Items.Add(li.Text);  
         }  
       }  
       if (CheckBoxList1.SelectedIndex == -1)  
       {  
         Label1.ForeColor = System.Drawing.Color.Red;  
       }  
       else  
       {  
         Label1.ForeColor = System.Drawing.Color.Black;  
       }  
       Label1.Text = ListBox1.Items.Count.ToString()+" items selected";  
     }  
   }  
 }  

No comments:

Post a Comment