asp.net.ph

Skip Navigation LinksHome > Getting Started > ASP.NET Primer > Introducing ASP.NET Web Forms

Introducing ASP.NET Web Forms

Authoring an ASP.NET Page   Introducing Web Forms Server Controls   Publishing ASP.NET Files


Web Forms is one of the key technologies in ASP.NET, designed to allow developers to easily create programmable Web pages.

Web Forms pages are created in a manner similar to static HTML pages, but include extra elements that ASP.NET recognizes and processes when the page runs.

The characteristics that distinguish ASP.NET Web pages from static HTML ( or other ) pages are as follows:

  • A file name extension of .aspx instead of .htm, .html, or other file name extensions. The .aspx file name extension causes the page to be processed by ASP.NET.
  • An optional @ Page directive or other directive, as appropriate to the type of page that you are creating.
  • A <form> element that is configured correctly for ASP.NET. The <form> element is required only if the page contains controls whose values you want to use during page processing.
  • Web server controls.
  • Server code, if you add your own code to the page.

You can rename any HTML page with the .aspx file name extension and it will run as an ASP.NET Web page.

However, if a page does not involve server processing, you do not need to add the .aspx file name extension, because doing so adds overhead to page processing.

The sections below provide more details on each of these elements.

Directives

ASP.NET pages usually contain directives that allow authors to specify page properties and configuration information for the page.

Directives are used by ASP.NET as instructions for how to process the page, but are not rendered as part of the markup that is sent to the browser.

The most commonly used directive is the @ Page directive, which specifies configuration options for the page, including the following:

  • The server programming language for code in the page.
  • Whether the page has an associated master page and should therefore be treated as a content page.
  • Debugging, tracing and other page configuration options.

If you do not include an @ Page directive in the page, or if the directive does not include a specific setting, settings are inherited from the configuration file for the Web application ( in the web.config file ) or from the site configuration file ( the machine.config file ).

In addition to the @ Page directive, you can include other directives that support additional page-specific options. For details, see Page Directives Syntax.

Form Elements

If your page includes controls that allow users to interact with the page and submit it, the page must include a <form> element.

You use the standard HTML <form> element, but certain rules apply.

  • The page can contain only one <form> element.
  • The <form> element must contain the runat="server" attribute.
  • Server controls that can perform a postback must be inside the <form> element.
  • The opening <form> tag must not contain an action attribute. ASP.NET sets these attributes dynamically when the page is processed, overriding any settings that you might make.

Web Server Controls

In most cases, your Web Form will be using one or more ASP.NET controls that allow users to interact with the page and submit it.

ASP.NET includes an extensive library of server controls you can use, from a simple textbox, selection list, button, and so on, to compound controls that can be used for displaying information from a data source.

The simpler server controls are similar to HTML <input> and <button> elements, except that they are processed on the server.

This allows you to use server code to set their properties. These controls also raise events that you can handle in server code.
Handling Control Action Events
Run Sample View Source

Server controls use a special syntax that ASP.NET recognizes when the page runs. For details, see Introducing Web Forms Server Controls.

HTML Elements as Server Controls

In addition to using ASP.NET server controls, you can use ordinary HTML elements as server controls, by simply adding the runat="server" attribute and an id attribute to uniquely identify the HTML element.

When the page runs, ASP.NET identifies the element as a server control and makes it available to server code. For example, you can add the required attributes to an HTML <span> element, as shown in the following.

<span runat="server" id="spanID">

You can then reference the <span> element in server code, for example to set its innerHtml attribute at run time in response to user input or from a given variable.
HTML Server Control Syntax Example
Run Sample | View Source

For more information, see ASP.NET HTML Controls.

Server Code

NOTE: The following discussion assumes you have a basic understanding of scripts. If not, Authoring an ASP.NET Page may help familiarize with the topic.

Again in most cases, your Web Form will include some type of server code, a set of instructions that tells ASP.NET to perform a certain task.

The task may be as simple as displaying the current date and time as shown in the example above, or more involved like retrieving and displaying data from a database.

Below shows a Web Form wherein users can search for items in a database. To test, type a string or any portion thereof that describes anything about house plans ( ex. elegant, stately, house, brick, plan, ranch, etc. ).
Searching for Items in a Database
Run Sample | View Source

Whatever the purpose of the code for the page is, the basic implementation is the same.

  • You can write server code within <script>...</script> tags contained within the page.
  • You can write server code in a separate file ( or code-behind file ), and declare a reference to the URL of the file on the page.

The examples shown earlier use scripts defined within the web page. Below is a simple example showing use of an external script file.
Using Code Behind Script Block
Run Sample | View Source

For more information, see ASP.NET Web Forms Overview.

See Also

Authoring an ASP.NET Page   Introducing Web Forms Server Controls   Publishing ASP.NET Files



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note