Computers & ProgrammingASP.NETBackend Development

Creating a Basic ASP.NET Page

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 .html or .htm.

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 runat="server".

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 <head> element.

Directives

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 .aspx or .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.

Directive Description
@ Page Defines page-specific attributes in .aspx files that are used by ASP.NET.
@ Control Defines control-specific attributes in .ascx files that are used by ASP.NET.
@ Import Imports a namespace into a page or user control.
@ Implements Indicates that a page or user control implements a specified .NET Framework interface.
@ Register 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.
@ Assembly Links an assembly to the current page during compilation, making all the assembly’s classes and interfaces available for use on the page.
@ Master Identifies and links the current page to an ASP.NET master page.
@ WebHandler Identifies an ASP.NET IHttpHandler page.
@ PreviousPageType Provides the means to get strong typing against the previous page as accessed through the PreviousPage property.
@ MasterType 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.
@ OutputCache Controls the output caching policies of a page or user control.
@ Reference Links a page or user control to the current page or user control.

Directive Examples

<%@ 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 .aspx file.

Leave a Comment

Your email address will not be published.

Scroll to Top