tag otherwise. Let’s consider the following example:
As is, it generates the following markup:
Sample text
If you set the label’s AssociatedControlID property to TextBox1, the markup changes as shown here:
Sample text
The run-time behavior changes a bit because now any click on the label text will be extended to the associated control. For example, clicking on the label will move the input focus to a text box, or it will select or deselect a check box.
Hidden Fields and File Upload
If you’re looking for a more comfortable programming interface to create hidden fields and upload files, two Web controls might help. The HiddenField and FileUpload controls add no new functionality to the ASP.NET programmer’s bag, but they have been added to the toolbox for completeness. A hidden field can be created in two other ways that work with ASP.NET 1.x too. For example, you can use the RegisterHiddenField method on the Page class:
// Works in ASP.NET 1.x but is obsolete starting with 2.0
RegisterHiddenField("HiddenField1", "Great book!");
Note that the RegisterHiddenField method has been flagged as obsolete as of ASP.NET 4. The recommended code analogous to the previous snippet is shown next:
// Recommended code
ClientScriptManager.RegisterHiddenField("HiddenField1", "Great book!");
In addition, to create a hidden field you can resort to the HTML markup, adding a runat attribute if you need to set the value programmatically:
Analogous considerations can be made for the FileUpload control, which provides the same capabilities as the HtmlInputFile control that we discussed earlier. In this case, though, the programming interface is slightly different and perhaps more intuitive. The HasFile property and SaveAs method hide any reference to the object that represents the posted file. Likewise, the FileName property provides a more immediate name for the name of the posted file. The code to upload a file can be rewritten as follows:
if (FileUpload1.HasFile)
{
// Get the name of the file to upload.
var fileName = FileUpload1.FileName;
var targetPath = GetSavePath(fileName); // a function of yours...
FileUpload1.SaveAs(targetPath);
}
Whether you use FileUpload or HtmlInputFile is mostly a matter of preference.
Miscellaneous Web Controls
The WebControls namespace also includes a few controls that provide useful functionality that is common in Web applications. In particular, we’ll examine the AdRotator control, which works like an advertisement banner, and the Calendar control, which is a flexible and highly interactive control used to specify a date.
The AdRotator Control
Abstractly speaking, the AdRotator control displays an automatically sized image button and updates both the image and the URL each time the page refreshes. The image to display and other information is read from an XML file written according to a specific schema. More concretely, you use the AdRotator control to create an advertisement banner on a Web Forms page. The control actually inserts an image and hyperlink in the page and makes them point to the advertisement page selected. The image is sized by the browser to the dimensions of the AdRotator control, regardless of its actual size. The following code shows a typical XML advertisement file:
6235.gif
www.microsoft.com/MSPress/books/6235.asp
Introducing ASP.NET AJAX
50
5727.gif