This article describes the principles of web form, the relationship between aspx and Aspx. CS, how to compile and parse. net, and how to render HTML. A single file, excluding the rear Code The usage and features of Aspx.
---------------------------------------------------------------------------------
A web forms page consists of two parts: visual elements (HTML, server controls, and static text) and the page's programming logic. visual Studio stores each of these components in a separate file. the visual elements are created in. aspx, and the code is in a separate class file, called the code-behind class file (. aspx. VB or. aspx. CS ).
It is also possible to create the visual elements and code in the same file, which is sometimes called a "single-file" web forms page. this style of web forms page has limited support in Visual Studio. for details, see "single-file web forms pages" below.
NoteVisual Studio does not support cross-language compiling in web forms pages and projects. this means it is not possible to include a Visual C # web forms page in a visual basic web project, and vice versa.
The ASP. NET page class
Although a web forms page consists of two separate files, they form a single unit when your application is run. the Code-behind class files for all web forms in a project are compiled into the dynamic-link library (. DLL) file produced by the project. the web forms. ASPX page file is also compiled, but somewhat differently. the first time a user browses to. ASPX page, Asp. net automatically generates. net class file that represents the page and compiles it to a second. DLL file. the generated class for. ASPX page inherits from the code-behind class that was compiled into the project. DLL file.
This single. DLL file is run at the server whenever the web forms page is requested. at run time, this. DLL file processes the incoming request and responds by dynamically creating output and sending it back to the browser or client device.
If the page contains server controls, as is typical, the derived page class acts as a container for the controls. instances of the controls are created at run time and likewise render output for the browser or client device.
For developers familiar with Active Server Pages (ASP), the ASP. NET page framework model represents something new. the ASP model is one of HTML extended with script code. the ASP page consists of script code, such as ecmascript (jscript, JavaScript) or VBScript, coexisting with static html within the same file. the ASP parser reads the page, interprets it, and runs only the script code to get output results. ASP then merges the output of the script code with the static html output found in the page before sending the output back to the browser or client device.
In the ASP. NETPageClass model, the entire web forms page is, in effect, an executable program that generates output to be sent back to the browser or client device. in this model, the page goes through a series of processing stages similar to those of other components-initialize, process, and dispose-with two differences:
- The page class performs these steps each time the page is called. The page is initialized, processed, and disposed of every time a round trip to the server occurs.
- The page class has a unique stage-render-that occurs toward the end of the page life cycle, during which output is generated. For more information, see control execution lifecycle.
NoteFor efficiency, the information required to recreate the page may be cached, but this action is independent of its life cycle.
Deriving from the page class
When Visual Studio creates the page and class files for a web forms page, it generates code that inherits from the basePageClass. For example, if you create a new web forms page and name it webpage1, a new class named webpage1 is derived fromSystem. Web. UI. Page.
Depending on whether you have chosen Visual Basic or Visual C # For your development, Visual Studio generates either of the following lines of code:
'Visual basic
Public class webform1
Inherits system. Web. UI. Page
// C #
Public class webform1: system. Web. UI. Page
The. ASPX page file in turn inherits from the derived webpage1 class. The relationship of the basePageClass, the derived class file, and the. aspx file is wrongly strated in the following disince.
Web forms Page Structure and the page base class
Because. aspx file is compiled dynamically when a user browses the page, its relationship to the class file is established with Script directives at the top of the page. in Visual Studio, the relationship. aspx files and class files is created and maintained automatically, even if you rename the web forms page. specifically,InheritsAttribute of@ PageDirective is used to specify the class file from which the. aspx file is derived. A typical directive looks like this:
<% @ Page Language = "VB" autoeventwireup = "false" codebehind = "webform1.aspx. VB"Inherits = "webapplication1.webform1
"%>
The codebehind, inherits, and SRC attributes
in web forms that use code-behind files, the @ page Directive (or @ Control in user control files) contains attributes that specify the relationship of. aspx file and its code-behind file. these attribute are:
- codebehind in Visual Studio, this attribute references the name of a file that contains the class for the page. for example, if you create a web forms page in Visual Studio called
webform1
, the codebehind attribute will point to webform1.aspx. VB
or webform1.aspx. CS
. this attribute is used only by the Visual Studio web forms designer. it tells the designer where to find the page class so that the designer can create an instance of it for you to work. the attribute is not used at run time.
- inherits identifies the class from which the page derives. in Visual Studio, this points to a class in the project assembly (. DLL), as shown in the dimo-abve.
The Code-behind model has strated abve is the model used by Visual Studio. the ASP. net Framework supports a slightly different code-behind model for web forms pages. in the ASP. net code-behind model, the visual elements are in. aspx file and the code is in a separate code-only file, as in Visual Studio. however, there is no project, and the code is not pre-compiled. instead, the code in. VB or. CS file is compiled at run time, when the page is first requested by a user.
The inheritance model works as your strated above, with the difference that the web forms class (webform1 class in the divisor) is not part of a project assembly. instead, each page is a separate assembly. there is no difference in how your code runs in the two models.
In the ASP. NET code-behind model, there is noCodebehindPage attribute, since that attribute is unique to Visual Studio. to tie the. aspx file to its corresponding code, the page directive containsSRCAttribute, which references the file containing the source code for the file.
TheSRCAttribute is not supported in Visual Studio. If you import a web forms page into Visual Studio that containsSRCAttribute, the designer will raise an error. For details, see the file cocould not be loaded into the web forms designer.
Single-file web forms pages
In addition to pages that are made up of. aspx file and a separate class file, the ASP. NET architecture supports a "single-file" model in which the UI elements and code are in the same file. single-file web forms pages are functionally very similar to pages consisting of two files. for example, you use the same controls on both types of pages. users still request pages using the same. aspx extension for the file name, and the page is still run using server-side code and it streams HTML to the client. A single-file page has the advantage that it can be easier to deploy.
There are a few differences in the way single-file pages are processed:
- The code for the page is not compiled into a separate class that the. aspx file then derives from. Instead, the. aspx file simply derives fromPageClass.
- When the page is deployed, the source code is deployed along with the web forms page, since it is physically in. aspx file. (the user does not see the code-only the results rendered when the page runs are sent to the user .)
You can work with single-file web forms pages in Visual Studio, but because Visual Studio is oriented toward the two-file, code-behind model, there is more limited support for single-file pages. differences include:
- You cannot directly create a single-file web forms page in Visual Studio. when you create a new page, by default Visual Studio creates a separate. aspx file and class file. to create a single-file page, you must initially create it as a text file and then change its extension. aspx.
-
- You cannot add non-visual components to the page (such as data components) by dragging them from the toolbox, because the web forms designer will not persist them in the page. instead, you shoshould add such components in code.
-
- You write code in HTML view, not in the code editor.
-
- When you are writing code, intelliisense is not supported-you do not get syntax checking or statement completion, tabbing, or code formatting.
- You must bind events to Event Handlers manually. for single-file web forms pages, Visual Studio does not support double-clicking to create the handler for a control's default event, nor the drop-down lists of classes and events in the code editor.
-
- Some debugging features, such as the ability to see a variable value by pointing the mouse at it, are not supported.
-
- Because the code in the page is not compiled into the project assembly, compile-time errors are not caught until the page runs.
For more information about single-file web forms pages, see working with single-file web forms pages in Visual Studio. NET.