ASP.NET by Example
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:
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 |