Creating ASP.NET web pages is very similar to creating basic HTML pages. There are some slight differences. First, ASP.NET web pages are text-based files saved with the file extension of
.aspx rather than
When a page with the file extension of
.aspx is requested, the web server (Internet Information Services, or IIS) will pass the request to the ASP.NET engine so that the HTML code that results from the process is sent back to the user agent (browser). At no time is ASP.NET code sent to the browser.
The web browser only understands HMTL markup. Another important difference about an ASP.NET page is that the content should be within one HTML form element that is configured with the attribute and value of
In addition, the
.aspx page can contain some additional ASP.NET specific markup such as directives at the top of the file, before the document declaration, or
DOCTYPE. For example, you can optionally specify
@ Page directive or other directives, web server controls, and server-side code.
Basic ASP.NET Web Page
Here is an example of a very basic ASP.NET web page. You will notice that the page mainly contains standard HTML elements.
<%@ Page Language="C#" %> <html> <body> <form id="Form1" runat="server"></form> </body> </html>
Keep in mind that this is a very simple example. There is quite a bit of markup missing if you are trying to ensure that the page would pass validation. For example, the
DOCTYPE is missing, as well as other important HTML elements such as the
In the basic example shown above, you will notice that the first line lists some specific ASP.NET syntax. This first line is known as a directive. When directives are used, they can be located anywhere in an
.ascx file. However, the standard practice is to include them at the beginning of the file.
Each directive can contain one or more attributes that are specific to that directive. The most common directive is the
Page directive, but as you continue to develop ASP.NET pages, you will become more familiar with others.
|Defines page-specific attributes in |
|Defines control-specific attributes in |
|Imports a namespace into a page or user control.|
|Indicates that a page or user control implements a specified .NET Framework interface.|
|Associates aliases with namespaces and class names, allowing user controls and custom server controls to be rendered when included in a requested page or user control.|
|Links an assembly to the current page during compilation, making all the assembly’s classes and interfaces available for use on the page.|
|Identifies and links the current page to an ASP.NET master page.|
|Identifies an ASP.NET |
|Provides the means to get strong typing against the previous page as accessed through the |
|Assigns a class name to the Master property of an ASP.NET page, so that the page can get strongly typed references to members of the master page.|
|Controls the output caching policies of a page or user control.|
|Links a page or user control to the current page or user control.|
<%@ Page attribute="value" [attribute="value"...] %> <%@ Control attribute="value" [attribute="value"...] %> <%@ Import namespace="value" %> <%@ Implements interface="ValidInterfaceName" %> <%@ Register tagprefix="tagprefix" tagname="tagname" src="pathname" %> <%@ Assembly Name="assemblyname" %> <%@ Master attribute="value" [attribute="value"...] %> <%@ WebHandler attribute="value" [attribute="value"...] %> <%@ PreviousPageType attribute="value" [attribute="value"...] %> <%@ MasterType attribute="value" [attribute="value"...] %> <%@ Reference Page="path to .aspx page" Control="path to .ascx file" virtualPath="path to file" %>
@ Page, Language Attribute
In order to process the server-side code contained in the ASP.NET page, the server needs to know what language the code will be in. The first line in the example above instructs ASP.NET that at runtime it should be expecting C# code for that page.
If your page contained VB.NET code, then the value of the language attribute should be set to
VB. While you can have some pages in your web application defined for C# and others for VB, you are not able to have both C# and VB code within the same page.
Do not worry too much about Directives or their syntax at this moment. Fortunately, Visual Studio automatically inserts many of these page directives when you perform certain actions such as adding a new