In addition to code and markup, ASP.net 2.0 pages can also contain server controls, which are programmable server-side objects that typically appear as UI elements (such as text boxes or images) on a page. The server control participates in the execution of the page and generates its own markup rendering for the client. The advantage of server controls is that it allows developers to derive complex rendering and operational behavior from simple, modular components, greatly reducing the amount of code needed to generate dynamic Web pages, and the other is that customizing their presentation and behavior is simple. The properties exposed by a server control can be set in a declarative (in markup) or programmatically (in code). The server control (and the page control itself) also exposes events that developers can handle in the course of a page execution, or in response to a client action (postback) that sends a page back to the server, the specific actions that are required to perform. Server controls also simplify the issue of reserving State information, which automatically preserves values between multiple successful "postback" operations.
A server control is declared in an. aspx file using a custom tag or an intrinsic HTML tag, and it contains the runat= "Server" property value. The intrinsic HTML markup is handled by a control in the System.Web.UI.HtmlControls namespace. Tags that are not explicitly mapped to a control are specified as System.Web.UI.HtmlControls.HtmlGenericControl types.
The following example uses four server controls:
, , , and . These server controls automatically generate HTML content at run time.
<form action="intro4_vb.aspx" method="post" runat=server>
<h3> Name: <asp:textbox id="Name" runat="server"/>
Category: <asp:dropdownlist id="Category" runat=server>
<asp:listitem >psychology</asp:listitem>
<asp:listitem >business</asp:listitem>
<asp:listitem >popular_comp</asp:listitem>
</asp:dropdownlist>
</h3>
<asp:button text="Lookup" runat="server"/>
</form>
Note that these server controls automatically retain the values entered by clients that travel to and from the server. These control states are not stored on the server (they are stored in the
In addition to supporting standard HTML input controls, ASP. NET also allows developers to use rich custom controls in their pages. For example, the following example shows how to use the control to dynamically display a scrolling advertisement on a page.
<form action="intro5_vb.aspx" method="post" runat="server">
<asp:adrotator AdvertisementFile="ads.xml" BorderColor="black" BorderWidth=1 runat="server"/>
<h3> Name: <asp:textbox id="Name" runat="server"/>
Category: <asp:dropdownlist id="Category" runat=server>
<asp:listitem >psychology</asp:listitem>
<asp:listitem >business</asp:listitem>
<asp:listitem >popular_comp</asp:listitem>
</asp:dropdownlist>
</h3>
<asp:button text="Lookup" runat="server"/>
</form>