File Structure

I l @ ve RuBoard

An ASP.NET page (as opposed to a control, service, and so on) can be written in any number of languages. The most common are Visual Basic and C#. An ASP.NET page generally has several items:

  • Page directives. Information provided to the compiler about the page. For example, a directive indicating that the page uses C# as its default language is written as follows :

     <%@ Page language="C#" %> 

    Directives normally appear at the top of the page, but may appear anywhere . (Page directives are detailed in the section "Page Directives.")

  • Scripting. Code that is executed either by the server or the client. Several different formats exist for server-executed code:

     <% Code written here is executed %> <%= Code written here is evaluated and the results are output %> <script runat="server" id="progID" language="VB">     Code written here </script> 

    Adding the runat="server" attribute and value instructs the server to execute the code when the page is called.

  • Comments. Text that either provides description or represents code that should not be executed, as indicated by the fact that it's surrounded by <%-- and %--> rather than <% %>:

     <%-- Code (or comments) written here will not be executed. --%> 
  • Controls. Predefined or custom code called via an XML element, sometimes including namespace information:

     <form method="post" runat="server">     <asp:label id="Label1" runat="Server"></asp:label> </form> 
  • Content. Neither code nor controls, represents text that is part of the resulting HTML page.

     <h1>Title of the Page</h1> 

Putting these components together results in the following ASP.NET page code:

 <%@ Page Language="VB" Trace="False" %> <script runat="server"> Sub Page_Load(Src As Object, E As EventArgs)     <%-- Set initial text for first_name control --%>     If Page.IsPostBack Then         output.Text = "You entered " & first_name.Text & "."     End If End Sub </script> <html> <head><title>Sample Form</title></head> <body> <asp:Label runat="server" id="output" /> <form runat="server" id="form1">     Please enter a name: <asp:Textbox runat="server" id="first_name" />     <asp:Button runat="server" Text="Submit" /> </form> </body></html> 

Page Parameters

The Page directive allows you to specify parameters that are used when the page is compiled. Only one Page directive is allowed on a page, so all parameters must be part of the same directive, as in the following example:

 <%@ Page AspCompat="true" Buffer="true" Trace="false" %> 

Available Page parameters are AspCompat , AutoEventWireup , Buffer , ClassName , ClientTarget , CodePage , CodeBehind , CompilerOptions , ContentType , Culture , Debug , Description , EnableSessionState , EnableViewState , EnableViewStateMac , ErrorPage , Explicit , Inherits , Language , LCID, ResponseEncoding , Src , Strict , Trace , TraceMode , Transaction , and WarningLevel .

The Page directive can only be used on an .aspx page.

Other Directives

The Page parameters are not the only page directives that can be used on an ASP.NET page. Other directives, such as Control (which can only be used in a user control), or Import , which imports a namespace or control into a page, use the same syntax as the Page parameters.

 <%@ Import Namespace="System.Net" %> <%@ Register TagPrefix="LoginModule" TagName="LoginModule" Src="login.ascx" %> 

Potential page directives are Page , Control , Import , Implements , Register , Assembly , OutputCache , and Reference . Each has its own list of parameters.

Adding Code-Behind Pages

One of the strengths of ASP.NET is the ability to build a page that is linked to external code files. The Inherits directive tells the compiler from which class your page inherits. The source code can reside in any file, but when your pages run, the server looks for the compiled code in the /bin directory or in the Global Application Cache (GAC). Visual Studio.NET, on the other hand, uses the CodeBehind page parameter to determine the organization of files in the project menu and, more importantly, where to find the code referenced in the Inherits directive. For example:

 <%@ Page language="c#" Inherits="MyPrj.StoredProc" CodeBehind="StoredProc.cs" %> 

You can then reference class functions from within the page. The server ignores the CodeBehind page parameter, but the Src parameter serves the same purpose, allowing the server to locate and compile the code. A page that carries both the CodeBehind and Src parameters can be used with certainty in both environments.

I l @ ve RuBoard


Asp. Net. By Example
ASP.NET by Example
ISBN: 0789725622
EAN: 2147483647
Year: 2001
Pages: 154

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net