Chapter 2: Server Controls


Overview

In Chapter 1, we talked about the ASP.NET page framework and briefly discussed ASP.NET server controls. In this chapter, we’ll look more closely at the server controls. Although we assume you have some familiarity with ASP.NET, we will begin this chapter by introducing a few server control basics and then look at some ways you can leverage that knowledge to accomplish the tasks associated with developing dynamic Web applications.

The server control object encapsulates a discrete piece of application functionality and is also typically responsible for producing the associated markup. The support for specifying a server control on a page is a blending of the familiar declarative tag structure of HTML markup and the ability to manipulate the object behavior at runtime in code.

Like the HTML page, an ASPX page is composed of a tree structure of elements. With a static HTML page, the markup is read by the browser and rendered to the user. The content of an ASPX page is first parsed into a tree of server controls that participate in the life cycle of the page, providing the opportunity to dynamically produce the markup sent to the browser. In fact, an HTML page can be turned into a server page by simply changing the file extension to .ASPX. A page like this would consist entirely of literal controls that render exactly as they appear in the source file, but this example illustrates why the server control architecture should feel natural to a Web developer.

Code Listing 2-1, HtmlHelloWorld.htm, is a simple HTML file that demonstrates the simplicity of the server controls concept.

Code Listing 2-1: HtmlHelloWorld.htm

start example
 <html>
<form>
<input type="text" value="Hello" />
<input type="submit" value="Go"/>
</form>
</html>
end example

If we rename the code in Code Listing 2-1 to HtmlHelloWorld.aspx, the markup sent to the client would be exactly the same, but the page would be parsed and compiled into a page class on the server. When requested, a tree of controls is built. In this case, the control tree consists of just the page object and a single literal control child that is the HTML content specified in the page. However, the control tree opens the door to manipulating the output dynamically. Later in this chapter, we’ll explore the life cycle of the server-side events for the controls.

There are two major types of server controls: HTML controls and Web controls. HTML controls provide a quick way to leverage existing knowledge of HTML while allowing you to easily add and manipulate dynamic features. Web controls typically have a bigger set of properties and methods that provide a higher degree of functionality encapsulation and expose richer programming features.




Microsoft ASP. NET Coding Strategies with the Microsoft ASP. NET Team
Microsoft ASP.NET Coding Strategies with the Microsoft ASP.NET Team (Pro-Developer)
ISBN: 073561900X
EAN: 2147483647
Year: 2005
Pages: 144

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net