Saturday, September 02, 2006

Structure of an ASP.NET Page

The following is a list of the important elements of an ASP.NET page:
  • Directives

  • Code declaration blocks

  • ASP.NET controls

  • Code render blocks

  • Server-side comments

  • Server-side include directives

  • Literal text and HTML tags

Directives

A directive controls how an ASP.NET page is compiled. The beginning of a directive is marked with the characters <%@ and the end of a directive is marked with the characters %>. A directive can appear anywhere within a page. By convention, however, a directive typically appears at the top of an ASP.NET page. There are several types of directives that you can add to an ASP.NET page. Two of the most useful types are page and import directives.

Code Declaration Blocks

A code declaration block contains all the application logic for your ASP.NET page and all the global variable declarations, subroutines, and functions. It must appear within a <Script Runat="Server"> tag. Note that you can declare subroutines and functions only within a code declaration block. You receive an error if you attempt to define a function or subroutine in any other section of an ASP.NET page.

ASP.NET Controls

ASP.NET controls can be freely interspersed with the text and HTML content of a page. The only requirement is that the controls should appear within a <form Runat= "Server"> tag. And, 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 pages is that they can contain only one <form Runat="Server"> tag. This means that you cannot group ASP.NET into multiple forms on a page. If you try, you get an error.

Code Render Blocks

If you need to execute code within the HTML or text content of your ASP.NET page, you can do so within code render blocks. The two types of code render blocks are inline code and inline expressions. Inline code executes a statement or series of statements. This type of code begins with the characters <% and ends with the characters %>. Inline expressions, on the other hand, display the value of a variable or method (this type of code is shorthand for Response.Write). Inline expressions begin with the characters <%= and end with the characters %>.

Server-side Comments

You can add comments to your ASP.NET pages by using server-side comment blocks. The beginning of a server-side comment is marked with the characters <%-- and the end of the comment is marked with the characters --%>. Server-side comments can be added to a page for the purposes of documentation. Note that you cannot see the contents of server-side comment tags, unlike normal HTML comment tags, by using the View Source command on your Web browser.

Server-side Include Directives

You can include a file in an ASP.NET page by using one of the 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 of the page including the file, you would 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 myDirectory under the wwwroot directory, you can include a file from that directory like this:

<!-- #INCLUDE virtual="/myDirectory/includefile.aspx" -->

Literal Text and HTML Tags

The final type of element that you can include in an ASP.NET page is HTML content. The static portion of your page is built with plain old HTML tags and text. You might be surprised to discover that the HTML content in your ASP.NET page is compiled along with the rest of the elements. HTML content in a page is represented with the LiteralControl class. You can use the Text property of the LiteralControl class to manipulate the pure HTML portion of an ASP.NET page.

Can't find what you're looking for? Try Google Search!
Google
 
Web eshwar123.blogspot.com

Comments on "Structure of an ASP.NET Page"