Cover | Table of Contents
The White Rabbit put on his spectacles. "Where shall I begin, please, your Majesty?""Begin at the beginning" the King said gravely, "and go on till you come to the end, then stop."Lewis CarrollAlice's Adventures in Wonderland, Chapter XII—
if statements, or method calls) may redirect the flow of the program, but essentially, once the program execution begins, it runs its course unaffected by anything the user or system may do. Before GUI environments, most computer programs were linear.Click event, and the page may have a method to handle the button's click event (such as Button1_Click). Your code in the event handler then responds to the button's being clicked in whatever way is appropriate for your application.Start and End), each session has events (again, such as Start and End), and the page and most of the server controls can raise events. All ASP.NET events are handled on the server. Some events cause an immediate posting to the server, and other events are stored until the next time the page is posted back to the server.<input>) is used for buttons, single-line text fields, checkboxes, hidden fields, and passwords. For multiline text fields, you must use the <textarea> tag. With ASP.NET server controls, each different type of functionality corresponds to a specific control. For example, all text is entered using the TextBox control; the number of lines is specified using a property. In fact, for ASP.NET server controls in
general, all the declared attributes correspond to properties of the class that represents the control.<h1>, <a>, and <input> are not processed by the server but are sent directly to the browser for display. Standard HTML controls can be exposed to the server and made available for server-side processing by turning them into HTML server controls.runat="server". In addition, you will probably want to add an id attribute, so the control contents can be accessed and controlled programmatically. For example, start with a simple input control:
<input type="text" size="40">
id and runat attributes, as follows:
<input type="text" id="BookTitle" size="40" runat="server">
runat attribute.OnClientClick, that lets you specify client-side script to execute when the button is clicked.onclick and onserverclick attributes for the HTML Button control for handling click events. Table 3-6 lists a few of the commonly used events available to HTML controls.
<%@ Page Language="C#" %>
<script runat="server">
void lblTime_Init(object sender, EventArgs e)
{
lblTime.Font.Name = "Verdana";
lblTime.Font.Size = 20;
lblTime.Font.Underline = true;
lblTime.Font.Bold = true;
lblTime.Font.Italic = true;
lblTime.Font.Overline = true;
lblTime.Font.Strikeout = true;
lblTime.Text = DateTime.Now.ToString() +
". Font Name: " +
lblTime.Font.Name;
}
</script>
<html>
<body>
<form id="form1" runat="server">
<h2>Basics</h2>
<asp:Label ID="lblTime" runat="server"
OnInit="lblTime_Init" />
</form>
</body>
</html>
Label control to display text. The Label control's Text property contains the text string to be displayed. Text is the only Label control property that is not inherited from the Control or WebControl classes. The Label control has no events or methods that are not derived from Control or WebControl.Label control used in code examples in previous chapters. You can set the Text and Font properties
of the Label control programmatically (as shown in Examples 4-1 and 4-3) or declaratively.TextBox control can be used for both user input and read-only text display. You can configure it to be single line or multiline or to accept passwords. If you set it to multiline, it automatically wraps unless the Wrap property is set to false. The text it contains can exceed the length of the control displayed on the page. The TextBox, DropDownList, Label, and other text-friendly controls implement the ITextControl interface, which is new to Version 2.0 of ASP.NET. This interface has a single property, Text, which is the visual content of the control.TextBox control. If any of these attributes are omitted from the control, then the default value will apply.|
Name
|
Type
|
Get
|
Set
|
Values
|
Description
|
|---|---|---|---|---|---|
AutoPostBack
|
Boolean
|
✗
|
<input type="hidden" value="foo">
value attribute encodes all the information saved in view state.<input type="hidden" name="_ _VIEWSTATE" value="/wEPDwUJL0CHlBR...YfL+BDX7xhMw=" />
id and runat attributes:<input type="hidden" value="foo" id="myHiddenControl" runat="server">
HiddenField control is best of all these options (assuming there is some compelling reason not to use ASP.NET's state capabilities) because it adds the following features:Value property, which holds the value being maintained by the controlClientID property, inherited from Control, which provides the ID attribute of the control itselfValueChangedButton controls, all members of the System.Web.UI.WebControls namespace:Button
LinkButton
LinkButton control is sort of a cross between a standard button and a HyperLink control (described in the next section). A LinkButton appears to the user as a hyperlink (i.e., the text is colored and underlined).
ImageButton
ImageButton control performs the same function as the standard button, except that an image bitmap takes the place of the button on the browser UI. For the ImageButton control, there is no Text attribute but there is an AlternateText attribute, which specifies what text to display on non-graphical browsers.ImageClickEventArgs event argument, which is different than the event handlers for the Button and LinkButton controls. This event argument exposes two fields (not used in this example) containing the X and Y coordinates of the location where the user clicked on the image. These fields could be used to implement your own image map type of functionality.HyperLink control looks similar to a LinkButton control with a fundamental difference: the HyperLink control immediately navigates to the target URL without a postback, while the LinkButton control posts the form. If the LinkButton event handler chooses, it will navigate to the target URL. A HyperLink control behaves very similarly to an HTML control.HyperLink control has four specific attributes:ImageUrl
ImageButton control, though the ImageButton control still posts the form and the HyperLink control only navigates.NavigateUrl
Text
Text and ImageUrl properties are both set, the ImageUrl takes precedence. The text will be displayed if the image is unavailable.ToolTip property (inherited from the WebControl class) has not been set, the Text value will display as a tool tip. If the ToolTip property has been set, the ToolTip text string will display as a tool tip.Target
CheckBox
CheckBoxList
CheckBox controls that can be dynamically created and bound to a data sourceRadioButton
RadioButtonList
RadioButton controls that can be dynamically created and bound to a data sourceListBox
DropDownList
ListBox but allows only a single selectionBulletedList
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.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 BulletedList
CheckBoxList
DropDownList
ListBox
RadioButtonList
ListControl and have much in common:ListItem (the information displayed by the list) works exactly the same way for all the ListControls, with a Value property and a Text property.Items property of the control contains the collection of all the ListItems.ListItems can be added to the Items collection either statically, i.e., declaratively in the content file, programmatically through the Add method, or from a data source.SelectedIndex and SelectedItem properties of the control point to the selected item with the lowest index. For single select controls, such as the DropDownList, the RadioButtonList, and the ListBox (if the SelectionMode property is set to ListSelectionMode.Single, the default value), the selected index is by definition, the lowest index. For multi-select controls, such as |
ASP.NET server control
|
HTML Analog
|
Description
|
|---|---|---|
Table
|
<table>
|
Parent control for
TableRow controls. The Rows property of the Table object is a collection of TableRow objects. |
TableRow |