By Jesse Liberty, Dan Hurwitz, Brian MacDonald
Book Price: $44.99 USD
£27.99 GBP
PDF Price: $30.99
Cover | Table of Contents | Colophon
Button control in the Toolbox, and drag it onto the design surface.Label control in the Toolbox, and drag that onto the design surface next to the button.
) on the toolbar
Button control?Label control do you use to set its content?Clickevent handler, and change the "Hello World" text to a message of support for your favorite sports team (or band, or movie, or whatever you like).Font property, and you'll find lots of options with which you're probably familiar. Try changing the font, the text size, and the color. You can also play with the border of the label too. Note that if you change the Text property here, you're changing the initial text of the label. After you've kicked the tires a bit, run your application to see how it looks. You can see an example in , although this is the affiliation of only one of the authors.
Label and Button. You've seen the Toolbox in the IDE, and it's stuffed with controls just waiting for you to experiment with. That's exactly what you're going to do in this chapter. You'll build a functional order form for a fictional business, even though you won't do anything just yet with the data your form will collect. You'll get to try out many of the basic controls in both Design view and Source view, you'll learn about web site fundamentals and selection controls and their collections of items, and you'll see how to display the results retrieved by one control in another control somewhere else on the page.ID property. Both Visual Web Developer and Visual Studio will automatically assign an ID to your control as you drag it onto your page. These automatically generated IDs are rarely meaningful, and we suggest that you rename them. For example, while the IDE might name your label "Label2," you will probably find it much more useful to rename that label lblPartialUpdate.
ScriptManager that is added by the IDE; we'll add AJAX starting in the next chapter.
<%@ Page AutoEventWireup="true" CodeFile="OrderForm.aspx.vb" Inherits="_Default"
Language="VB" %>
<!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>AdventureWorks</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<h1>
AdventureWorks Order Form
</h1>
<table>
<tr>
<td >
Customer Name:
</td>
<td >
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td >
Address:
</td>
<td >
<asp:TextBox ID="txtAddress" runat="server" />
</td>
</tr>
<tr>
<td >
City:</td>
<td >
<asp:TextBox ID="txtCity" runat="server"
Font-Bold="True" Font-Names="Arial Black" />
</td>
</tr>
<tr>
<td >
State:</td>
<td >
<asp:DropDownList ID="ddlState" runat="server">
<asp:ListItem Value="AL">Alabama</asp:ListItem>
<asp:ListItem Value="AK">Alaska</asp:ListItem>
<asp:ListItem Value="CA">California</asp:ListItem>
<asp:ListItem Value="CT">Connecticut</asp:ListItem>
<asp:ListItem Value="FL">Florida</asp:ListItem>
<asp:ListItem Value="PA">Pennsylvania</asp:ListItem>
<asp:ListItem Value="TX">Texas</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td >
Zip:</td>
<td style="width: 100px">
<asp:TextBox ID="txtZip" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td >
E-mail:
</td>
<td >
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td >
Password:
</td>
<td >
<asp:TextBox ID="txtPassword" runat="server"
TextMode="Password" />
</td>
</tr>
<tr>
<td>
Comment:</td>
<td >
<asp:TextBox ID="txtComment" runat="server"
Rows="3" TextMode="MultiLine" Width="300px" />
</td>
</tr>
</table>
</div>
<br />
Provide personal information:
<asp:RadioButton ID="rbYes" runat="server" AutoPostBack="True "
Checked="True" GroupName="grpPersonalInfo"
Text="Yes"
ToolTip="Click Yes to gather personal information - no to skip that step" />
<asp:RadioButton ID="rbNo" runat="server" AutoPostBack="True"
GroupName="grpPersonalInfo"
Text="No"
ToolTip="Click Yes to gather personal information -no to skip that step " />
<asp:Panel ID="pnlPersonalInfo" runat="server"
BorderWidth="1px" Width="300px" BackColor="beige">
<table>
<tr valign="top">
<td>
Areas of Interest<br />
<asp:CheckBoxList ID="cblAreas"
runat="server" AutoPostBack="True" Width="150px">
<asp:ListItem>Biking</asp:ListItem>
<asp:ListItem>Scuba Diving</asp:ListItem>
<asp:ListItem>Gaming</asp:ListItem>
<asp:ListItem>Mountain Climbing</asp:ListItem>
<asp:ListItem>Web Surfing</asp:ListItem>
<asp:ListItem>Real Surfing</asp:ListItem>
</asp:CheckBoxList></td>
<td>
Age Category
<br />
<asp:RadioButtonList ID="rblAge" runat="server"
AutoPostBack="True" Width="150px">
<asp:ListItem>Under 21</asp:ListItem>
<asp:ListItem>Under 21</asp:ListItem>
<asp:ListItem>21 to 30</asp:ListItem>
<asp:ListItem>31 to 50</asp:ListItem>
<asp:ListItem>Over 50</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
</table>
</asp:Panel>
<br />
<table>
<tr valign="top">
<td>
Product Category:</td>
<td style="width: 100px">
<asp:DropDownList ID="ddlCategory" runat="server"
ToolTip="Select a category">
<asp:ListItem>Bikes</asp:ListItem>
<asp:ListItem>Components</asp:ListItem>
<asp:ListItem>Clothing</asp:ListItem>
<asp:ListItem>Accessories</asp:ListItem>
<asp:ListItem>Scuba</asp:ListItem>
<asp:ListItem>Parasailing</asp:ListItem>
</asp:DropDownList></td>
<td>
SubCategory:</td>
<td >
<asp:ListBox ID="lbSubCategory" runat="server"
ToolTip="Select a sub-category">
<asp:ListItem>Brakes</asp:ListItem>
<asp:ListItem>Handlebars</asp:ListItem>
<asp:ListItem>Chains</asp:ListItem>
<asp:ListItem>Cranks</asp:ListItem>
<asp:ListItem>Bottom Brackets</asp:ListItem>
<asp:ListItem>Tires</asp:ListItem>
<asp:ListItem>Wheels</asp:ListItem>
<asp:ListItem>Seats</asp:ListItem>
<asp:ListItem>Derailleurs</asp:ListItem>
</asp:ListBox></td>
</tr>
</table>
<br />
<asp:CheckBox ID="cbDisplayPhoto"
runat="server"
AutoPostBack="True"
Checked="True"
Text="Show product photo?"
TextAlign="Left" />
<asp:Image ID="imgPhoto"
runat="server"
ImageUrl="Dan at Vernal Pool.jpg" />
<br /><br />
Summary<br />
<table>
<tr>
<td style="height: 21px" >
Category:</td>
<td>
<asp:Label ID="CategoryLabel" runat="server" Text="" />
</td>
</tr>
<tr>
<td >
SubCategory:</td>
<td>
<asp:TextBox ID="SubCategoryTextBox" runat="server"
ReadOnly="true" />
</td>
</tr>
<tr valign="top">
<td style="height: 21px" >
Mailing Address:</td>
<td id="tdAddress" runat="server" style="width:100px; height: 21px;">
</td>
</tr>
</table>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<br />
<br />
For help, contact
<asp:HyperLink
ID="hypContact"
runat="server"
NavigateUrl="http://www.LibertyAssociates.com"
Target="_blank">
Liberty Associates, Inc.
</asp:HyperLink>
<br />
</form>
</body>
</html>
ID property. The IDE assigns a default ID automatically, but you can (and usually should) rename them to be more meaningful.TextBox control is a relatively simply control that allows the user to enter text that you can retrieve later. You can change the TextMode property of a TextBox to create single-line entry fields, multiline fields, or to hide the text for a password field.selection controls, including the ListBox, DropDownList, RadioButton, RadioButtonList, CheckBox, and CheckBoxList, which display various options for the user to choose from. You decide which control to use based on its appearance, and whether you want the user to be able to make only one selection from within a list or multiple selections.AutoPostBack property of a control is set to True, the page is posted back to the server whenever that control's value changes.Panel control for?SelectedItem property retrieve?HyperLink control open in a new window?UpdatePanel control is. Open the Postbacks web site, similar to how you opened HelloWorld in the previous exercise. In Design view, drag another UpdatePanel control inside the first one, after the button. Drag another Label control inside the new UpdatePanel. In the Properties window, set the label's name to lblOtherPartialUpdate, and set its width to 200px. (Note that you can't give this label the same name as the other label—or any other control on the page—or you'll get an error.) Now add another Button to the new UpdatePanel, under the label, set its name to btnOtherPartialUpdate, and change the text to "Another partial-page update:".lblOtherPartialUpdate.Text = DateTime.Now
ScriptManager control, ensuring that you have access to a fully tested, reliable control that manages the "grunt" work. Adding a ScriptManager control to your page (which the IDE does automatically when you create an AJAX-enabled project) solves the problem, and having one on the page that you don't need comes at virtually no cost. Here is the declaration that must appear in every page, and which is put there for you by the IDE:<asp:ScriptManager ID="ScriptManager1" runat="server" />
ScriptManager control will also be visible in Design view, as shown in , but will not be visible when the web site is run.ScriptManager's EnablePartialRendering property set to its default value of True.UpdatePanel controls onto your page. Each UpdatePanel is updated individually and asynchronously, without affecting one another or anything else on the page.
Panel control whose purpose was to collect personal information. You'll enable that feature now.UpdatePanel, however, the update will be done asynchronously, and there will be no page flicker.Toolkit Control | Description |
|---|---|
Accordion | A control that provides multiple panes, only one of which is visible at a time |
AlwaysVisibleControlExtender | Keeps a control visible as the user scrolls the page |
AnimationExtender | Helps you animate a panel or other control on your page |
CascadingDropDown | The user's selection from one drop down control determines the choices available in the next drop down |
CollapsiblePanelExtender | Allows any Panel to collapse and expand |
ConfirmButtonExtender | When the user clicks a button, a dialog box pops up to confirm the choice |
DragPanelExtender | Lets the user drag a panel around on the page |
FilteredTextBoxExtender | Ensures that only "valid" text may be entered into a text box |
HoverMenuExtender | Pops up a menu when the mouse hovers over a control |
MutuallyExclusive-CheckBoxExtender | Pick none or one of several checkboxes. This provides functionality similar to radio buttons, but with the ability to uncheck all the checkboxes |
NoBot | A control which attempts to prevent spam or robot interaction with a web site |
NumericUpDownExtender | Adds up/down functionality to a TextBox control; can cycle through numeric values or from a list of provided values |
PagingBulletedListExtender | Extends a BulletedList control to provide client-side sorted paging |
PasswordStrength | Helps the user pick a good password |
Rating | Quick rating, allowing a user to pick the number of stars out of a maximum number (you could use this, for example, to set up a 4-star [or 5-star, or 10-star] rating system for restaurant or movie reviews) |
ReorderList | Lets the user reorder the members of a list by dragging them into place |
TextBoxWaterMarkExtender | Displays helpful text in a textbox until you start to type |
UpdatePanelAnimationExtender | Quick animation of a panel; move, resize, fade |
ValidatorCalloutExtender | Extends ASP.NET validation controls by displaying a warning with an image if
the field isn't valid |
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="OrderForm.aspx.vb"
Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
TagPrefix="cc1" %>
<!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">
<style type="text/css">@import url(StyleSheet.css);</style>
<title>AdventureWorks CollapsiblePanelExtender</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<h1>AdventureWorks Order Form</h1>
<table>
<tr>
<td style="width: 100px">
Customer Name:</td>
<td style="width: 100px">
<asp:TextBox ID="txtName" runat="server"
CssClass="unwatermarked">
</asp:TextBox>
<cc1:TextBoxWatermarkExtender runat="server"
ID="CustomerNameWatermark"
TargetControlID="txtName"
WatermarkCssClass="watermarked"
WatermarkText="Your name">
</cc1:TextBoxWatermarkExtender>
</td>
</tr>
<tr>
<td style="width: 100px">
Address:</td>
<td style="width: 100px">
<asp:TextBox ID="txtAddress" runat="server"
CssClass="unwatermarked"></asp:TextBox>
<cc1:TextBoxWatermarkExtender runat="server"
ID="CustomerAddressWatermark"
TargetControlID="txtAddress"
WatermarkCssClass="watermarked"
WatermarkText="Home address">
</cc1:TextBoxWatermarkExtender>
</td>
</tr>
<tr>
<td style="width: 100px">
City:</td>
<td style="width: 100px">
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 100px">
State:</td>
<td style="width: 100px">
<asp:DropDownList ID="ddlState" runat="server">
<asp:ListItem Value="AL">Alabama</asp:ListItem>
<asp:ListItem Value="AK">Alaska</asp:ListItem>
<asp:ListItem Value="CA">California</asp:ListItem>
<asp:ListItem Value="CT">Connecticut</asp:ListItem>
<asp:ListItem Value="FL">Florida</asp:ListItem>
<asp:ListItem Value="PA">Pennsylvania</asp:ListItem>
<asp:ListItem Value="TX">Texas</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 100px">
Zip:</td>
<td style="width: 100px">
<asp:TextBox ID="txtZip" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 100px">
E-mail:</td>
<td style="width: 100px">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 100px">
Password:</td>
<td style="width: 100px">
<asp:TextBox ID="txtPassword" runat="server"
TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
Comment:</td>
<td style="width: 100px">
<asp:TextBox ID="txtComment" runat="server" Rows="3"
TextMode="MultiLine" Width="300px"></asp:TextBox>
</td>
</tr>
</table>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Provide personal information:
<asp:RadioButton ID="rbYes" runat="server" AutoPostBack="True"
Checked="True"
GroupName="grpPersonalInfo"
Text="Yes" ToolTip="Do gather personal info" />
<asp:RadioButton ID="rbNo" runat="server" AutoPostBack="True"
GroupName="grpPersonalInfo"
Text="No" ToolTip="Do not gather personal info" /><br />
<asp:Panel ID="pnlPersonalInfo" runat="server" BorderWidth="1px">
<table>
<tr valign="top">
<td >
Areas of Interest<br />
<asp:CheckBoxList ID="cblAreas" runat="server"
AutoPostBack="True" Width="150px">
<asp:ListItem>Biking</asp:ListItem>
<asp:ListItem>Scuba Diving</asp:ListItem>
<asp:ListItem>Gaming</asp:ListItem>
<asp:ListItem>Mountain Climbing</asp:ListItem>
<asp:ListItem>Web Surfing</asp:ListItem>
<asp:ListItem>Real Surfing</asp:ListItem>
</asp:CheckBoxList></td>
<td style="width: 121px">
Age Category<br />
<asp:TextBox ID="txtAgeCategory" runat="server"
Width="175px">
</asp:TextBox><br />
<cc1:TextBoxWatermarkExtender
ID="TextBoxWatermarkExtender1" runat="server"
TargetControlID="txtAgeCategory"
WatermarkText="Click here for age categories">
</cc1:TextBoxWatermarkExtender>
<cc1:PopupControlExtender ID="PopupControlExtender1"
runat="server"
TargetControlID="txtAgeCategory"
PopupControlID="pnlAgeCategories"
Position="Bottom">
</cc1:PopupControlExtender>
<asp:Panel ID="pnlAgeCategories" runat="server"
Height="50px" Width="125px">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:RadioButtonList ID="rblAge" runat="server"
AutoPostBack="True" Width="150px"
OnSelectedIndexChanged="rblAge_SelectedIndexChanged">
<asp:ListItem Value="Under 21 - Enjoy it!">Under 21
</asp:ListItem>
<asp:ListItem Value="21 to 30 - Livin' Large">21 to 30
</asp:ListItem>
<asp:ListItem Value="31 to 50 - Life Is Good">31 to 50
</asp:ListItem>
<asp:ListItem Value="Over 50 - Golden Years">Over 50
</asp:ListItem>
</asp:RadioButtonList>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</td>
</tr>
</table>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:Panel ID="pnlProductInfoHeader" runat="server"
Height="50px" Width="125px">
<asp:Image ID="imgProductInfo_ToggleImage" runat="server"
ImageUrl="collapse.jpg" /><br />
Product Information</asp:Panel>
<asp:Panel ID="pnlProductInfo" runat="server" BackColor="LightGray"
Width="450px">
<asp:Label ID="myLabel" runat="server" Text=
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Aliquam eleifend, turpis sit amet tincidunt euismod, urna eros mattis
neque, vitae facilisis nulla dui ut dolor. Proin pretium. Etiam ultrices
eleifend neque. Mauris vestibulum purus quis nibh. Phasellus dignissim.
Vivamus laoreet magna id purus. In hac habitasse platea dictumst. Vivamus
congue elit quis arcu. Sed lorem mauris, convallis non, porta sed, interdum
id, nisl. Aenean id tortor. Sed ac quam. Suspendisse ornare luctus sapien.
Praesent aliquet, lacus nec venenatis placerat, massa metus mattis dolor,
non eleifend pede sapien et lorem. Curabitur dapibus faucibus nunc.">
</asp:Label>
<br />
<br />
<asp:Image ID="Image1" runat="server"
ImageUrl="Dan at Vernal Pool.jpg" />
</asp:Panel>
<cc1:CollapsiblePanelExtender ID="cpeProductInfo" runat="server"
CollapseControlID="pnlProductInfoHeader"
Collapsed="true"
CollapsedImage="expand.jpg"
CollapsedText="Product Information (Show Details...)"
ExpandControlID="pnlProductInfoHeader"
ExpandedImage="collapse.jpg"
ExpandedText="Product Information (Hide Details...)"
ImageControlID="imgProductInfo_ToggleImage"
SuppressPostBack="true"
TargetControlID="pnlProductInfo">
</cc1:CollapsiblePanelExtender>
</form>
</body>
</html>
ScriptManager control is the key control that makes ASP.NET AJAX possible by managing the JavaScript for you. The control is placed on everyAJAX-enabled page by default, and its EnablePartialRendering property is set to True, so you don't need to do anything yourself.UpdatePanel control enables you to update those controls without posting back to the server.TargetControlID property that you use to set the existing control that the extender is extending.TextBoxWaterMarkExtender adds a watermark effect to an existing textbox, providing a prompt for the reader to enter data.TextBoxWaterMarkExtender can apply a separate style to a text box, if you have a style sheet defined for the project, or it can just add the text you specify.PopupControlExtender can help you make efficient use of the space on your page, by hiding some content until the user clicks on a control.CollapsiblePanelExtender to a regular Panel control, causing it to hide most of its content until the user clicks on it. The Panel then expands, displaying its content, until the user collapses it.