Directives control the compilation of an ASP.NET page. The beginning of a directive is marked with the characters <%@ and the end with %> character. This is shown in line 1 of the example above. A directive can appear anywhere in the page but by convention, a directive appears typically at the top of an ASP.NET page. There are two types of directives:
This directive appears on the top of an ASP.NET page. It specifies which programming language is being used to write an ASP.NET page. For example if we want to use VB as our page language, we will use the following directive:
And if it is C# then we use the following directive:
By default, only certain namespaces are automatically imported into an ASP.NET page. Referring to a class that isn’t a member of one of the default namespaces, you must explicitly import the namespace of the class. For example look at this directive:
|<%@ Import Namespace=”System.Web.Mail” %>
This line imports all of the classes of the System.Web.Mail namespace, to send the mail through the SmtpMail class. This class is contained in the System.Web.Mail namespace as shown in the Example 5.1 below:
<%@ Import Namespace="System.Web.Mail" %>
Dim mailMessage As SmtpMail
"Sending Mail!” _
The output of this example is shown below:
In this example, the first line contains an import directive. Without the import directive, the page would generate an error because it would not be able to resolve the SmtpMail class.
Code Declaration Block
You can see that line 3 to 7 (of Example 5 Message.aspx) contains a block of code called a code declaration block. This looks like the client server code. This is the code that ASP.NET uses to process its pages, and it is here that you will handle all the web pages functionally. This code is also compiled into MSIL.
Code Render Block
Starting from line 9(of Example 5 Message.aspx), the HTML page begins. This contains the code contents that will be sent to the browser (along with whatever your ASP.NET code spits out).
Line 13 (of Example 5 Message.aspx) begins with <%. This is known as the code render block. It contains additional instructions that ASP.NET pages uses to produce output. In the above case, it tells the ASP.Net page to write, “This is ASP.NET Page” to the browser.
Code render blocks are not as efficient as code declaration blocks. These blocks are not compiled.
ASP.NET controls can be freely interspersed with the text of HTML content of a page. The only requirement is that, the controls should appear within a <form Runat=”Server”> tag as shown in line 16 (of Example 5 Message.aspx). For certain tags such as <span Runat=”Server”> and <ASP:Label Runat=”Server”/>, this requirement can be ignored without any dire consequences.
One significant limitation of ASP.NET page is that it contains only one <form Runat=”Server”> tag. This means that you can not group ASP.NET into multiple forms on a page. And if you try this you will get an error.
Server Side Comments
Line 25 to 29 (of Example 5 Message.aspx) shows the server side comments code. Comments can be added to ASP.NET page by using server-side comment blocks. The beginning of the server side comment is marked with the character <%-- and the end with characters --%>. Server side comments can be added to a page for the purpose of documentation.
Note that you can not see the contents of server-side comment tags, unlike normal HTML comment tags, by using the View Source command on your browser.
Server side comments are also useful when you are debugging an ASP.NET page. You can temporarily remove both ASP.NET controls and code render blocks from a page by surrounding these elements with server side comments.
Server Side Include Directives
You can include a file in an ASP.NET page by using one of two forms of the server side include directive. If you want to include a file that is located in the same directory or in a subdirectory you can use the following directive:
|<! -- #INCLUDE file=”includefile.aspx” -->
Alternatively, you can include a file by supplying the full virtual path. For example, if you have a subdirectory named expertDirectory under the wwwroot directory, you can include a file from that directory like this:
|<! -- #INCLUDE virtual/expertDirectory/includefile.aspx” -->