The main components of an ASP.NET page are:
• Code Declaration Blocks
• ASP.NET Controls
• Code Render Blocks
• Server-Side Comments
• Server-Side Include Directives
• Literal Text and HTML Tags.
A directive controls how the page is compiled. It is marked by the tags, <%@ and %>. It can appear anywhere in a page. But, normally it is placed at the top of a page. The main types of directives are:
A Page directive is used to specify the default programming language for a page.
<%@ Page Language="C#" %>
<%@ Language="C#" %>
Some namespaces are imported into an ASP.NET page by default. If you wish to use a class that is not contained in the default namespaces, you must import its namespace.
<%@ Import Namespace="System.Data.SqlClient" %>
A code declaration block contains all the application logic for a page. It also includes declarations of global variables, and functions. It must be written within the script runat= "server" tag.
The <script> tag has two optional parameters:
• Language: You can specify the programming language to be used within the <script> tag. Otherwise, the language specified in the Page directive is used.
• SRC: You can specify an external file that contains the code block.
There is no difference in output between writing the code on the same page and including an external script file.
ASP.NET controls can be mixed with text and static HTML in a page. All controls must appear within a <form runat= "server"> tag. Some controls such as <span runat= "server"> and the Label control can appear outside this tag. You can have only one form per page in ASP.NET.
If you wish to execute code within HTML, you can include the code within code render blocks. There are two types of code render blocks:
• Inline Code: It executes a statement or series of statements. It is marked by the characters <% and %>.
• Inline Expressions: They display the value of a variable or method. They can be considered as shorthand notation for the Response.Write method. They are marked by the characters <%= and %>.
You can add comments in server-side code using the characters <%-- and --%>. The main use of these comment blocks is to add documentation to a page.
You can include a file in an ASP.NET page by using a server-side include directive. It is executed before any of the code in the page. If the file is in the same directory or in a sub-directory of the page including the file, this directive is written as:
<!-- #INCLUDE file="includedfile.aspx" -->
You can also specify the virtual path of the file. To include a file located in the directory MyAspx under the wwwroot directory, you will write the directive as:
<!-- #INCLUDE virtual="/MyAspx/includedfile.aspx" -->
Note: It is recommended that you avoid using server-side include directives. It is better to use user controls.
You can build the static part of an ASP.NET page using HTML tags and literal text. The HTML content of your page is also compiled along with the rest of the contents.
The literal text has been made bold and converted to uppercase before being rendered in the browser.