Section 4.7. Selecting Values

4.7. Selecting Values

Several ASP.NET server controls allow the user to select a value or values:



CheckBox

Allows selection of Boolean data



CheckBoxList

Group of CheckBox controls that can be dynamically created and bound to a data source



RadioButton

Allows only a single option to be selected from a group



RadioButtonList

Group of RadioButton controls that can be dynamically created and bound to a data source



ListBox

Allows selection of one or more items from a predefined list



DropDownList

Similar to a ListBox but allows only a single selection



BulletedList

Formatted with bullets and can be simple text or a link

All of these controls derive from the WebControl class. The RadioButton derives further from the CheckBox class, and the list controls all derive from the abstract ListControl class. Each of these controls is considered in detail in upcoming sections.

4.7.1. CheckBox Control

A CheckBox control provides a means for a user to select Boolean data (i.e., Yes/No or True/False). If you have several checkboxes arranged together (not to be confused with a CheckBoxList , discussed next ), you can select multiple options. No option is mutually exclusive of another.

The CheckBox and RadioButton controls implement the ICheckBoxControl interface, which is new to ASP.NET Version 2.0. This interface provides for a single property called Checked , and a single event called CheckedChanged , both of which are described next.

The following example, CheckBoxDemo , shown in Figure 4-6, demonstrates the use of three independent CheckBoxes to control the appearance of a Label . Clicking any of the checkboxes in this exampleUnderline, Overline, or Strikeoutimposes that font attribute on the text string in the Label control.

Figure 4-6. CheckBoxDemo

The content file is shown in Example 4-12.

Example 4-12. Default.aspx from CheckBoxDemo
 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"          Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>CheckBox Control</title> </head> <body>     <form id="form1" runat="server">     <div>       <h1>CheckBox Control</h1>        <asp:Label ID="lblTime" runat="server"                   OnInit="lblTime_Init" />        <br />        <br />  <asp:CheckBox ID="chkUnderLine" runat="server"                      AutoPostBack="True"                      Text="Underline?"                      TextAlign="Left"                      OnCheckedChanged="chkUnderLine_CheckedChanged" />        <asp:CheckBox ID="chkOverLine" runat="server"                      AutoPostBack="True"                      Text="Overline?"                      OnCheckedChanged="chkOverLine_CheckedChanged" />        <asp:CheckBox ID="chkStrikeout" runat="server"                      AutoPostBack="True"                      Text="Strikeout?"                      OnCheckedChanged="chkStrikeout_CheckedChanged" />  </div>     </form> </body> </html> 

Each of the ASP.NET server controls in this example, the label and the three checkboxes, has an event handler. The Init event for the Label is handled to set the format and content of the label every time the page is posted. The CheckBoxes have the default CheckedChanged event handled. This event passes a standard EventArgs argument, which does not expose any properties.

All these event handler methods are contained in the code-behind file, listed in Example 4-13.

Example 4-13. Event handlers in Default.aspx.cs code-behind file for CheckBoxDemo
 protected void lblTime_Init(object sender, EventArgs e) {    lblTime.Font.Name = "Verdana";    lblTime.Font.Size = 20;    lblTime.Font.Bold = true;    lblTime.Font.Italic = true;    lblTime.Text = DateTime.Now.ToString(); } protected void chkUnderLine_CheckedChanged(object sender, EventArgs e) {    if (chkUnderLine.Checked)       lblTime.Font.Underline = true;    else       lblTime.Font.Underline = false; } protected void chkOverLine_CheckedChanged(object sender, EventArgs e) {    if (chkOverLine.Checked)       lblTime.Font.Overline = true;    else       lblTime.Font.Overline = false; } protected void chkStrikeout_CheckedChanged(object sender, EventArgs e) {    if (chkStrikeout.Checked)       lblTime.Font.Strikeout = true;    else       lblTime.Font.Strikeout = false; } 

Like all controls derived from WebControl , CheckBoxes have an ID property. But as the sample code in Example 4-12 shows, there are several other properties and methods that are not inherited from WebControl . These members are listed in Table 4-4. However, some of these properties, such as AutoPostBack and Text , are common to several other controls.

Table 4-4. Members of the CheckBox class not inherited from WebControl control class

Name

Type

Get

Set

Values

Description

AutoPostBack

Boolean

true , false

Determines if automatic postback to the server will occur if the user changes the contents of the control. If false (the default), postback to the server will not occur until the page is posted, either by a button or other postback control or a control with AutoPostBack set to true .

Checked

Boolean

true , false

Indicates if the CheckBox is checked. Default is false .

Text

String

 

The text label associated with the CheckBox .

TextAlign

TextAlign

Left , Right

Dictates if the text label is on the left or right of the CheckBox . Default is Right .

CheckedChanged

Event

   

EventArgs

This event is raised when the Checked property is changed. This event will not immediately post back to the server unless AutoPostBack is set to TRue .


4.7.2. RadioButton Control

A RadioButton control is very similar to, and in fact is derived from, a CheckBox control. The essential difference between the two classes is that RadioButtons are typically grouped using the GroupName property, and only one RadioButton in the group can be checked (i.e., its Checked property is true ) at one time. Changing the Checked property of one RadioButton control in the group to true changes the Checked property of all other controls in the group to false . In addition, radio buttons typically display as round, as opposed to the square checkboxes.

The next example, RadioButtonDemo, contains three RadioButton controls to set the font size of a label. Each of the radio buttons in RadioButtonDemo are part of the group grpSize .

The content file for this example is shown in Example 4-14, and the event handlers in the code-behind file are in Example 4-15. The result of running it is shown in Figure 4-7.

Figure 4-7. RadioButtonDemo

Example 4-14. Default.aspx for RadioButtonDemo web site
 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs"          Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>RadioButton Control</title> </head> <body>     <form id="form1" runat="server">     <div>       <h1>RadioButton Control</h1>        <br />        <asp:Label ID="lblTime" runat="server"                   OnInit="lblTime_Init"></asp:Label>        <br />        <br />  <asp:RadioButton ID="rdoSize10" runat="server"                         GroupName="grpSize"                         AutoPostBack="True"                         Text="10pt"                         OnCheckedChanged="grpSize_CheckedChanged" />        <asp:RadioButton ID="rdoSize14" runat="server"                         GroupName="grpSize"                         AutoPostBack="True"                         Text="14pt"                         OnCheckedChanged="grpSize_CheckedChanged" />        <asp:RadioButton ID="rdoSize16" runat="server"                         GroupName="grpSize"                         AutoPostBack="True"                         Text="16pt"                         OnCheckedChanged="grpSize_CheckedChanged" />  </div>     </form> </body> </html> 

Example 4-15. Event handlers in Default.aspx.cs code-behind file for RadioButtonDemo
  protected void grpSize_CheckedChanged(object sender, EventArgs e) {    if (rdoSize10.Checked)       lblTime.Font.Size = 10;    else if (rdoSize14.Checked)       lblTime.Font.Size = 14;    else lblTime.Font.Size = 16; }  protected void lblTime_Init(object sender, EventArgs e) {    lblTime.Font.Name = "Verdana";    lblTime.Font.Size = 20;    lblTime.Font.Bold = true;    lblTime.Font.Italic = true;    lblTime.Text = DateTime.Now.ToString(); } 

The CheckedChanged event, which is derived from CheckBox , is handled by the onCheckedChanged event handler, which points to the grpSize_CheckedChanged method. That method is an if..else block that changes the text size depending on which button is selected. In practice, it would probably be better to use a C# switch statement to make it easier to add additional radio buttons in the future .



Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 173

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net