ASP 4.0 CheckBox Control
In the Previous Lab #007, we learnt about the Command Buttons. Now, we will explore how to use ASP 4.0 CheckBox Control. The CheckBox Control is suitable to get a true or false choice from the user. For Example: A CheckBox showing the text Smoker expects only one input from the user, which has two choices (Yes or No). To show the text, one should use the Text property of the CheckBox Control. When the user clicks the CheckBox Control, it will raise the CheckedChanged Event. By default, the Checkbox will not do any Postback, but it can be changed via PostBack property. In this Lab #008, we will explore about the ASP 4.0 CheckBox Control.
1: Create WebForm – CheckBox Control
The below picture shows the Sub-Steps involved. The experienced gained in the previous labs helps you to follow these steps easily:
Before you proceed with the steps, create a WebForm called 04_CheckBoxControl.aspx. You can either use the website from the previous lab and add the web form or create a new empty website and add the Form. Now follow the sub-steps:
1) Enter the Free text: Pick Food Type and apply whatever formatting you like.
2) Create a
CheckBox in the Form with CheckBox Label Fish.
3) Set Arial Font.
4) Set size as Large.
5) Set Text property as Fish. You can either use the Property Tab or Type the attributes in the source view or Even copy paste the content from the Reference Code section.
6) Unlike other two CheckBoxes, this CheckBox Control first shows the caption text. This is because of the
TextAlign property set to Left.
7) One can set this property in the Source View or through the property window.
8) Below the CheckBox controls, place a Label Control. This control reports the Food preference submitted by the user.
9) Properties for the Label control are shown here. Note, you need to change the ID property to lblDisplay which is missed in this screenshot.
2: Handle CheckedChanged Event
In this step, we will handle the CheckedChanged Event of the CheckBox Control. Refer to the steps screen:
1) Pick the CheckBox Control labelled Egg and set its
AutoPostBack property to true. By default, CheckBox will not do any postback and it depends on the form submit action. But, by setting this attribute (Property) to true, the CheckBox will submit the data to the form when its check state changes.
2) After setting AutoPostBack property, handle the
CheckedChanged Event (Handling Events explained in the previous Labs).
[Repeat Steps 1 & 2 for the other two CheckBoxes]
3) Next, write a function called GetFoodPref
as a class member (Refer Code Reference section). This function forms the string based on the CheckBox state. Also, all the CheckedChanged event handler calls this function to form the Food Preference String. Marker 3 shows how we check a check box is in checked state or not via the
Checked Property. Asp will set this property to true when the checkbox is ticked by the user. Similar if else
condition will check the state of the other two Check Boxes to form the Food Preference string.
4) Shows the Checked-Changed event handler for the Fish CheckBox.
5) In the event handler, we set the Label control text with the Food Preference. The event handler reads the same for the other two CheckBoxes as well.
3. Running the CheckBox ASP 4.0 Example
The Screenshot below shows what you can do with this example:

1) Start the Website from Visual Studio. Notice that all the CheckBox Controls displayed on the Web Form. The Blue Label is not yet updated.
2) Place a check mark on the check box labelled Fish. This performs a postback and runs the event routine on the server.
3) The server forms the food preference string and returns that to the web browser control. You can see the label gets the update from the server as part of the Post back. Note, the server remembers the previous check mark on the Fish. This is an important feature of the Postback, and the server puts back the control state when it is sending the reply to the browser.
4) Place a check mark on the check box labelled Egg.
5) Notice the Food preference changes.
4. Code Reference
04_CheckBoxControl.aspx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="04_CheckBoxControl.aspx.cs" Inherits="_04_CheckBoxControl" %> <!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> <style type="text/css"> .style1 { font-family: Arial; font-size: x-large; } </style> </head> <body> <form id="form1" runat="server"> <div class="style1"> Pick Food Type<br /> </div> <asp:CheckBox ID="ChkFish" runat="server" Font-Names="Arial" Font-Size="Large" Text="Fish" AutoPostBack="True" oncheckedchanged="ChkFish_CheckedChanged" /> <br /> <asp:CheckBox ID="ChkMeat" runat="server" Font-Names="Arial" Font-Size="Large" Text="Meat" AutoPostBack="True" oncheckedchanged="ChkMeat_CheckedChanged" /> <br /> <asp:CheckBox ID="ChkEgg" runat="server" Font-Names="Arial" Font-Size="Large" Text="Egg" TextAlign="Left" AutoPostBack="True" oncheckedchanged="ChkEgg_CheckedChanged" /> <br /> <asp:Label ID="lblDisplay" runat="server" Font-Names="Arial Black" ForeColor="Blue" Text="Label"></asp:Label> </form> </body> </html> |
04_CheckBoxControl.aspx.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _04_CheckBoxControl : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public string GetFoodPref() { //Sample 01: Form the String reply of Food Choice string ret = "My Food Preference :- </br>"; if (ChkFish.Checked == false) { ret = ret + "No Fish </br>"; } else { ret = ret + "With Fish </br>"; } if (ChkMeat.Checked == false) { ret = ret + "No Meat </br>"; } else { ret = ret + "With Meat </br>"; } if (ChkEgg.Checked == false) { ret = ret + "No Egg </br>"; } else { ret = ret + "With Egg </br>"; } return ret; } //Sample 02: Handle Checked Changed Event protected void ChkFish_CheckedChanged(object sender, EventArgs e) { lblDisplay.Text = GetFoodPref(); } protected void ChkMeat_CheckedChanged(object sender, EventArgs e) { lblDisplay.Text = GetFoodPref(); } protected void ChkEgg_CheckedChanged(object sender, EventArgs e) { lblDisplay.Text = GetFoodPref(); } } |
Categories: Asp 2.0
Tags: CheckBox, Checked, CheckedChanged, TextAlign