Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [194]

By Root 5563 0
and correctly handled, the typical ASP.NET Web Forms page contains a single
tag decorated with the runat attribute set to server. During server-side processing, such a tag is mapped to an instance of the HtmlForm class. The HtmlForm class acts as the outermost container of all server controls and wraps them in a plain HTML element when the page is rendered. The resulting HTML form posts to the same page URL. By design, it doesn’t give you any chance to set the action URL programmatically, and for this reason it is often said to be reentrant. The default method used to submit form data is POST, but GET can be used as well.

In most cases, the server form is the outermost tag of the page and is contained directly in . In general, though, the server tag can be the child of any other server container control, such as ,
, , and any other HTML generic control. (I covered HTML controls and Web controls in Chapter 6.) If any noncontainer server controls (for example, a TextBox) are placed outside the form tag, an exception is thrown as the page executes—no check is made at compile time. The exception is raised by the control itself when the host page begins to render. Noncontainer Web controls, in fact, check whether they are being rendered within the boundaries of a server form and throw an HttpException if they are not. A call to the Page’s VerifyRenderingInServerForm method does the job. (Be aware of this virtuous behavior when you get to writing custom controls.)

In this chapter, we’ll examine some aspects of form-based programming in ASP.NET, including how to use multiple forms in the same page and post data to a different page. We’ll also touch on input validation and validation controls.

Programming with Forms


One of the most common snags Web developers face when they first approach the ASP.NET lifestyle is the fact that managed Web applications support the single-form interface model. In the single-form interface model, each page always posts to itself and doesn’t supply any hook for developers to set the final destination of the postback. What in HTML programming is the Action property of the form is simply not defined on the ASP.NET HtmlForm class. By default, each ASP.NET page can post only to itself, unless some specific API extensions are used to perform a cross-page post. Unlike the action URL, the HTTP method and the target frame of the post can be programmatically adjusted using ad hoc HtmlForm properties—Method and Target.

The HtmlForm Class


The HtmlForm class inherits from HtmlContainerControl, which provides the form with the capability of containing child controls. This capability is shared with other HTML control classes, such as HtmlTable, characterized by child elements and a closing tag.

Properties of the HtmlForm Class


The HtmlForm class provides programmatic access to the HTML element on the server through the set of properties shown in Table 9-1. Note that the table includes only a few of the properties HtmlForm inherits from the root class Control.

Table 9-1. Form Property

Property

Description

Attributes

Inherited from Control, gets a name/value collection with all the attributes declared on the tag.

ClientID

Inherited from Control, gets the value of UniqueID.

Controls

Inherited from Control, gets a collection object that represents the child controls of the form.

DefaultButton

String property, gets or sets the button control to display as the default button on the form.

DefaultFocus

String property, gets or sets the button control to give input focus when the form is displayed.

Disabled

Gets or sets a value indicating whether the form is disabled. It matches the disabled HTML attribute.

EncType

Gets or sets the encoding type. It matches the enctype HTML attribute.

ID

Inherited from Control, gets or sets the programmatic identifier of the form. The default value is aspnetForm.

InnerHtml

Inherited from HtmlContainerControl, gets or sets the markup content found between the opening

®Online Book Reader