CheckBox Example in ASP.NET using C#

CheckBox Example in ASP.NET using C#


Posted in : ASP.Net Posted on : November 20, 2010 at 6:49 PM Comments : [ 0 ]

In this article we will introduce with the CheckBox control in ASP.NET using C#.

CheckBox Example in ASP.NET using C#

CheckBox control in ASP.NET is used to select or deselect the value. ChechBox is just like a RadioButton control in HTML. The difference between a CheckBox and a RadioButton is that once you have selected a checkbox you are able to deselect it by clicking on it again. In this example we use three ChechBox control. You can choose all of three CheckBoxs or any selection which you want. After that click on select button your selected products will be displayed.

CheckBox.aspx (Design page):

CheckBox.aspx (source code):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" 
AutoEventWireup="true" CodeFile="CheckBox.aspx.cs" Inherits="CheckBox" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.style3
{
width: 300px;
height:120px;
}
.tdStyle:
{
width:150px;
height:30px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
<h2 style="color:Green">CheckBox in ASP.NET 4 , C#</h2>
<table cellpadding="0" cellspacing="0" class="style3">
<tr>
<td class="tdStyle">
<strong>Products</strong></td>
<td class="tdStyle">
<strong>Selected Products</strong></td>
</tr>
<tr >
<td class="tdStyle">
<asp:CheckBox ID="CheckBox1" runat="server" Font-Bold="True" 
Font-Names="Shruti" ForeColor="#145765" 
Text="Tea" />
</td>
<td>
<asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="#000099"></asp:Label>
</td>
</tr>
<tr>
<td class="tdStyle">
<asp:CheckBox ID="CheckBox2" runat="server" Font-Bold="True" 
Font-Names="Shruti" ForeColor="#145765" 
Text="Coffee" />
</td>
<td>
<asp:Label ID="Label2" runat="server" Font-Bold="True" ForeColor="#000099"></asp:Label>
</td>
</tr>
<tr>
<td class="tdStyle">
<asp:CheckBox ID="CheckBox3" runat="server" Font-Bold="True" 
Font-Names="Shruti" ForeColor="#145765" 
Text="Burger" />
</td>
<td>
<asp:Label ID="Label3" runat="server" Font-Bold="True" ForeColor="#000099"></asp:Label>
</td>
</tr>
</table>
<br />
<asp:Button ID="button1" runat="server" Text="Select" onclick="button1_Click" 
Width="76px" />
</div>
</asp:Content>

CheckBox.apsx.cs (C# code file):


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class CheckBox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (CheckBox1.Checked == true)
{
Label1.Text = "Tea selected";
}
else
{
Label1.Text = "";
}
if (CheckBox2.Checked == true)
{
Label2.Text = "Coffee selected";
}
else
{
Label2.Text = "";
}
if (CheckBox3.Checked == true)
{
Label3.Text = "Burger selected";
}
else
{
Label3.Text = "";
}
}
protected void button1_Click(object sender, EventArgs e)
{

}
}

Output:

   

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics