Finally, the TextBox control supports the Focus method.. You can use the Focus method to shift the initial form focus to a particular TextBox control.. If you want to make it easier for
Trang 1AutoCompleteType=”LastName”
Runat=”server” />
<br /><br />
<asp:Button
id=”btnSubmit”
Text=”Submit”
Runat=”server” />
</div>
</form>
</body>
</html>
FIGURE 2.7 Using AutoComplete with the TextBox control
NOTE
When using Internet Explorer, you can configure AutoComplete by selecting Tools,
Internet Options, Content, and clicking the AutoComplete button The ASP.NET Framework
does not support AutoComplete for other browsers such as FireFox or Opera
Finally, the TextBox control supports the Focus() method You can use the Focus()
method to shift the initial form focus to a particular TextBox control By default, no form
field has focus when a page first opens If you want to make it easier for users to complete
a form, you can set the focus automatically to a particular TextBox control contained in a
form
Trang 2For example, the page in Listing 2.9 sets the focus to the first of two form fields
LISTING 2.9 TextBoxFocus.aspx
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<script runat=”server”>
void Page_Load()
{
txtFirstName.Focus();
}
</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>TextBox Focus</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<asp:Label
id=”lblFirstName”
Text=”First Name:”
AssociatedControlID=”txtFirstName”
Runat=”server” />
<br />
<asp:TextBox
id=”txtFirstName”
AutoCompleteType=”FirstName”
Runat=”server” />
<br /><br />
<asp:Label
id=”lblLastname”
Text=”Last Name:”
AssociatedControlID=”txtLastName”
Runat=”server” />
<br />
<asp:TextBox
id=”txtLastName”
AutoCompleteType=”LastName”
Runat=”server” />
<br /><br />
<asp:Button
Trang 3id=”btnSubmit”
Text=”Submit”
Runat=”server” />
</div>
</form>
</body>
</html>
In Listing 2.9, the Page_Load() event handler sets the form focus to the txtFirstName
TextBox control
NOTE
You can also set the form focus by setting either the Page.SetFocus() method or the
server-side HtmlForm control’s DefaultFocus property
Using the CheckBox Control
The CheckBox control enables you to display, well, a check box The page in Listing 2.10
illustrates how you can use the CheckBox control in a newsletter signup form (see
Figure 2.8)
LISTING 2.10 ShowCheckBox.aspx
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<script runat=”server”>
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblResult.Text = chkNewsletter.Checked.ToString();
}
</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>Show CheckBox</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<asp:CheckBox
id=”chkNewsletter”
Trang 4Text=”Receive Newsletter?”
Runat=”server” />
<br />
<asp:Button
id=”btnSubmit”
Text=”Submit”
OnClick=”btnSubmit_Click”
Runat=”server” />
<hr />
<asp:Label
id=”lblResult”
Runat=”server” />
</div>
</form>
</body>
</html>
FIGURE 2.8 Displaying a CheckBox control
In Listing 2.10, the Checked property determines whether the user has checked the check
box Notice that the CheckBox includes a Text property that labels the CheckBox If you use
this property, the proper (accessibility standards-compliant) HTML <label> tag is
gener-ated for the TextBox
Trang 5The CheckBox control supports the following properties (this is not a complete list):
server automatically when the CheckBox is checked or unchecked
Text—Enables you to provide a label for the check box
and Right
The CheckBox control also supports the following method:
And the CheckBox control supports the following event:
Notice that the CheckBox control, like the TextBox control, supports the AutoPostBack
property The page in Listing 2.11 illustrates how you can use the AutoPostBack property
to post the form containing the check box back to the server automatically when the
check box is checked or unchecked
LISTING 2.11 CheckBoxAutoPostBack.aspx
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<script runat=”server”>
protected void chkNewsletter_CheckedChanged(object sender, EventArgs e)
{
lblResult.Text = chkNewsletter.Checked.ToString();
}
</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>CheckBox AutoPostBack</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
Trang 6<asp:CheckBox
id=”chkNewsletter”
Text=”Receive Newsletter?”
AutoPostBack=”true”
OnCheckedChanged=”chkNewsletter_CheckedChanged”
Runat=”server” />
<hr />
<asp:Label
id=”lblResult”
Runat=”server” />
</div>
</form>
</body>
</html>
NOTE
The ASP.NET Framework also includes the CheckBoxList control that enables you to
display a list of check boxes automatically This control is discussed in detail in
Chapter 10, “Using List Controls.”
Using the RadioButton Control
You always use the RadioButton control in a group Only one radio button in a group of
RadioButton controls can be checked at a time
For example, the page in Listing 2.12 contains three RadioButton controls (see Figure 2.9)
LISTING 2.12 ShowRadioButton.aspx
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<script runat=”server”>
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (rdlMagazine.Checked)
lblResult.Text = rdlMagazine.Text;
if (rdlTelevision.Checked)
lblResult.Text = rdlTelevision.Text;
if (rdlOther.Checked)
lblResult.Text = rdlOther.Text;
Trang 7}
</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>Show RadioButton</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
How did you hear about our Website?
<ul>
<li>
<asp:RadioButton
id=”rdlMagazine”
Text=”Magazine Article”
GroupName=”Source”
Runat=”server” />
</li>
<li>
<asp:RadioButton
id=”rdlTelevision”
Text=”Television Program”
GroupName=”Source”
Runat=”server” />
</li>
<li>
<asp:RadioButton
id=”rdlOther”
Text=”Other Source”
GroupName=”Source”
Runat=”server” />
</li>
</ul>
<asp:Button
id=”btnSubmit”
Text=”Submit”
Runat=”server” OnClick=”btnSubmit_Click” />
<hr />
<asp:Label
id=”lblResult”
Runat=”server” />
Trang 8</div>
</form>
</body>
</html>
FIGURE 2.9 Displaying RadioButton
The RadioButton controls in Listing 2.12 are grouped together with the RadioButton
control’s GroupName property Only one of the three RadioButton controls can be
checked at a time
The RadioButton control supports the following properties (this is not a complete list):
the server automatically when the radio button is checked or unchecked
Text—Enables you to label the RadioButton control
Right
Trang 9The RadioButton control supports the following method:
Focus—Enables you to set the initial form focus to the RadioButton control
Finally, the RadioButton control supports the following event:
CheckedChanged—Raised on the server when the RadioButton is checked or
unchecked
The page in Listing 2.13 demonstrates how you can use the AutoPostBack property with a
group of RadioButton controls and detect which RadioButton control is selected
LISTING 2.13 RadioButtonAutoPostBack.aspx
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<script runat=”server”>
protected void RadioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton selectedRadioButton = (RadioButton)sender;
lblResult.Text = selectedRadioButton.Text;
}
</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>RadioButton AutoPostBack</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
How did you hear about our Website?
<ul>
<li>
<asp:RadioButton
id=”rdlMagazine”
Text=”Magazine Article”
GroupName=”Source”
AutoPostBack=”true”
OnCheckedChanged=”RadioButton_CheckedChanged”
Runat=”server” />
</li>
<li>
<asp:RadioButton
Trang 10id=”rdlTelevision”
Text=”Television Program”
GroupName=”Source”
AutoPostBack=”true”
OnCheckedChanged=”RadioButton_CheckedChanged”
Runat=”server” />
</li>
<li>
<asp:RadioButton
id=”rdlOther”
Text=”Other Source”
GroupName=”Source”
AutoPostBack=”true”
OnCheckedChanged=”RadioButton_CheckedChanged”
Runat=”server” />
</li>
</ul>
<hr />
<asp:Label
id=”lblResult”
Runat=”server” />
</div>
</form>
</body>
</html>
In Listing 2.13, when you select a RadioButton control, the page is automatically posted
back to the server, and the value of the Text property of the selected RadioButton control
displays Notice that all three of the RadioButton controls are associated with the same
CheckedChanged event handler The first parameter passed to the handler represents the
particular RadioButton that was changed
NOTE
The ASP.NET Framework also includes the RadioButtonList control, which enables you
to display a list of radio buttons automatically This control is discussed in detail in
Chapter 10