Saturday, June 14, 2014

CheckBoxlist Control

If we want the user to select more than one option, then a CheckBoxList control can be used.
Usefull properties

1. RepeatDirection
2. RepeatColumns
3. Enabled
4. SelectedIndex :- If we select the multiple item It only returns the selected index of the one list item object (lowest level index).
5. SelectedValue :- If we select the multiple item It only returns the selected value of the one list item object (lowest level index).
6. SelectedItem :- It returns only one item.

Example :-
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_23_CheckBoxList.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" RepeatColumns="2"   
       RepeatDirection="Horizontal">  
       <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:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  
     <br />  
   </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 _23_CheckBoxList  
 {  
   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 (CheckBoxList1.SelectedItem != null)  
       {  
          Response.Write(CheckBoxList1.SelectedItem.Text);  
       }  
       /*foreach (ListItem li in CheckBoxList1.Items)  
       {  
         if (li.Selected)  
         {  
           Response.Write("Text = " + li.Text + ", ");  
           Response.Write("Value = " + li.Value + ", ");  
           Response.Write("Index = " + CheckBoxList1.Items.IndexOf(li));  
           Response.Write("<br/>");  
         }  
       }*/  
     }  
   }  
 }  

No comments:

Post a Comment