Check Box Code to Display the Result of Combination in ASP.NET

Default.aspx

  1. <html>  
  2.   
  3. <head runat="server">  
  4.     <title></title>  
  5. </head>  
  6.   
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.         <div align="center">  
  10.             <div style="width:500px">  
  11.                 <h1 style="border:2px solid green">Welcome to Check Box Example</h1>  
  12.                 <asp:Panel ID="panel" runat="server">  
  13.                     <asp:CheckBox ID="CheckBox1" runat="server" Text="Option1" />    
  14.                     <asp:CheckBox ID="CheckBox2" runat="server" Text="Option2" />    
  15.                     <asp:CheckBox ID="CheckBox3" runat="server" Text="Option3" />    
  16.                     <asp:CheckBox ID="CheckBox4" runat="server" Text="Option4" />    
  17.                     <asp:CheckBox ID="CheckBox5" runat="server" Text="Option5" />  
  18.                     <br /><br />  
  19.                     <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />  
  20.                     <br /><br />  
  21.                     <asp:Label ID="lblStatus" Style="cursive" runat="server" Text="Label"></asp:Label>  
  22.                 </asp:Panel>  
  23.             </div>  
  24.         </div>  
  25.     </form>  
  26. </body>  
  27.   
  28. </html>   

Default.aspx.cs

  1. using System;  
  2.   
  3. using System.Web;  
  4.   
  5. using System.Web.UI;  
  6.   
  7. using System.Web.UI.WebControls;  
  8.   
  9.   
  10.   
  11. public partial class _Default: System.Web.UI.Page  
  12.   
  13. {  
  14.   
  15.     protected void Page_Load(object sender, EventArgs e)  
  16.   
  17.     {  
  18.   
  19.         lblStatus.Visible = false;  
  20.   
  21.     }  
  22.   
  23.     protected void Button1_Click(object sender, EventArgs e)  
  24.   
  25.     {  
  26.   
  27.         lblStatus.Visible = true;  
  28.   
  29.         string strSelectedOption = string.Empty;  
  30.   
  31.         foreach(Control chkitem in panel.Controls)  
  32.   
  33.         {  
  34.   
  35.             if (chkitem.GetType().Name == "CheckBox")  
  36.   
  37.             {  
  38.   
  39.                 CheckBox chk = (CheckBox)(chkitem);  
  40.   
  41.                 if (chk.Checked == true)  
  42.   
  43.                 {  
  44.   
  45.                     strSelectedOption += chk.Text + ", ";  
  46.   
  47.                 }  
  48.   
  49.             }  
  50.   
  51.   
  52.         }  
  53.   
  54.         if (strSelectedOption != "")  
  55.   
  56.         {  
  57.   
  58.             strSelectedOption = strSelectedOption.Remove(strSelectedOption.Length - 1);  
  59.   
  60.             int pos = strSelectedOption.LastIndexOf(", ");  
  61.   
  62.             int x = 0;  
  63.   
  64.             if (pos != -1)  
  65.   
  66.             {  
  67.   
  68.                 strSelectedOption = strSelectedOption.Remove(pos, 1);  
  69.   
  70.                 strSelectedOption = strSelectedOption.Insert(pos, " and ");  
  71.   
  72.             }  
  73.   
  74.             string str1 = strSelectedOption.Remove(strSelectedOption.LastIndexOf(","));  
  75.   
  76.             string s = str1 + str1.Replace(str1, ".");  
  77.   
  78.             lblStatus.Text = "<b>Selected Check box : </b>" + s + " ";  
  79.   
  80.         } else  
  81.   
  82.         {  
  83.   
  84.             lblStatus.Text = "<b style='color:red'>Please select at least one check box</b>";  
  85.   
  86.         }  
  87.   
  88.     }  
  89.   
  90. }