Sideway
output.to from Sideway
Draft for Information Only

Content

ASP.NET Page
 ASP.NET Web Page Code Model
  The Single-File Page Model
   Examples of Visual C# ASP.NET Single-File Page Model
   Examples of Visual Basic ASP.NET Single-File Page Model
  The Code-Behind Page Model
   Note
   Note
   Examples of Visual C# ASP.NET Code-Behind Page Model
   Examples of Visual Basic ASP.NET Code-Behind Page Model
  Choosing a Page Model
   Note
  Advantages of Single-File Pages
  Advantages of Code-Behind Pages
  Compilation and Deployment
  See Also
  Concepts
 Sources and References

ASP.NET Page

ASP.NET Web Page Code Model

 An ASP.NET Web page consists of two parts:
  • Visual elements, which include markup, server controls, and static text.
  • Programming logic for the page, which includes event handlers and other code.
ASP.NET provides two models for managing the visual elements and code — the single-file page model and the code-behind page model. The two models function the same, and you can use the same controls and code for both models. This topic explains how each model functions and provides suggestions for when to choose one model or the other.

The Single-File Page Model

 In the single-file page model, the page's markup and its programming code are in the same physical .aspx file. The programming code is in a script block that contains the attribute runat="server" to mark it as code that ASP.NET should execute. The following code example shows a single-file page containing a Button control and a Label control. The highlighted portion shows the Click event handler for the Button control inside a script block.

Examples of Visual C# ASP.NET Single-File Page Model

ASP.NET Code Input:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <title>Sample Page</title>
       <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
       <%@ Page Language="C#" %>
       <script runat="server">
           protected void Button1_Click(Object sender, EventArgs e)
           {
               Label1.Text="Clicked at "+DateTime.Now.ToString();
           }
       </script>
    </head>
    <body>
       <form id="form1" runat="server">
           <h3>Single-File Page Model</h3>
           <div>

               <asp:Label id="Label1"
                   runat="server" Text="Label">
               </asp:Label>

             <br /><br />

               <asp:Button id="Button1"
                   Text="Button"
                   OnClick="Button1_Click"
                   runat="server">
               </asp:Button>
           </div>
       </form>
    </body>
</html>
HTTP Response Output:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <title>Sample Page</title>
       <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    </head>
    <body>
       <form method="post" action="./aspnetpage_codemodel_01a_01.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTE5MDUzMTQ2MGRkL2WnhJNyy7HDd3R4SqRDOLQd9nTIXKuXQd9BEDh4VqQ=" />
</div>

<div class="aspNetHidden">

	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="11621844" />
	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAKh3TbSZ18C7HxhUkhlAWyizfg78Z8BXhXifTCAVkevd0Z89BP4Twdjaut+858ZHCd2MdJTvw/CLF17sS5JA1GH" />
</div>
           <h3>Single-File Page Model</h3>
           <div>

               <span id="Label1">Label</span>

             <br /><br />

               <input type="submit" name="Button1" value="Button" id="Button1" />
           </div>
       </form>
    </body>
</html>
ASPX Web Page Embedded Output:

Examples of Visual Basic ASP.NET Single-File Page Model

ASP.NET Code Input:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <title>Sample Page</title>
       <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
       <%@ Page Language="vb" %>
       <script runat="server" >
           Protected Sub Button1_Click(ByVal sender As Object,  ByVal e As System.EventArgs)
               Label1.Text="Clicked at " & DateTime.Now.ToString() 
           end sub
       </script>
    </head>
    <body>
       <form id="form1" runat="server">
           <h3>Single-File Page Model</h3>
           <div>
               <asp:Label id="Label1"
                   Text="Label"
                   runat="server">
               </asp:Label>
               <br /><br />
               <asp:Button id="Button1"
               Text="Button"
               OnClick="Button1_Click"
               runat="server">
               </asp:Button>
           </div>
       </form>
    </body>
</html>
HTTP Response Output:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <title>Sample Page</title>
       <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    </head>
    <body>
       <form method="post" action="./aspnetpage_codemodel_01a_02.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTE5MDUzMTQ2MGRkNCElAjmaU+FOizIFbZrHvTAytOvSxXon3TQv3F0urRw=" />
</div>

<div class="aspNetHidden">

	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="5055C2E2" />
	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAJjIpS0m1Nf6YaJTl1H2t8hzfg78Z8BXhXifTCAVkevd+gghj5WRi3IUIAQ7/Bpyj8LzWcoI0qHXCJNpUPs63a4" />
</div>
           <h3>Single-File Page Model</h3>
           <div>
               <span id="Label1">Label</span>
               <br /><br />
               <input type="submit" name="Button1" value="Button" id="Button1" />
           </div>
       </form>
    </body>
</html>
ASPX Web Page Embedded Output:
The scriptblock can contain as much code as the page requires. The code can consist of event handlers for controls on the page (as in the example), methods, properties, and any other code that you would normally use in a class file. At run time, a single-file page is treated as a class that derives from the Page class. The page does not contain an explicit class declaration. Instead, the compiler generates a new class that contains the controls as members. (Not all controls are exposed as page members; some are children of other controls.) The code in the page becomes part of the class; for example, event handlers that you create become members of the derived Page class.
Because all code in the page becomes members of the Page class, you cannot have a using statement in your code. Instead, add a @ Import directive that specifies the namespace to import. You might also need to add a reference to the DLL that contains the namespace.
For more information, see ASP.NET Page Class Overview.

The Code-Behind Page Model

 The code-behind page model allows you to keep the markup in one file—the .aspx file—and the programming code in another file. The name of the code file varies according to what programming language you are using.

Note

 Not all .NET programming languages allow you to create code-behind files for ASP.NET Web pages. Languages must support partial classes. For example, J# does not support partial classes, and therefore does not support creating code-behind files for ASP.NET pages.
For example, if you are working with a page named SamplePage, the markup is in the file SamplePage.aspx and the code is in a file named SamplePage.aspx.vb (for Visual Basic), SamplePage.aspx.cs (for C#), and so on.

Note

 The code-behind model used in the .NET Framework version 2.0 is different than the one used in earlier versions.
In the code-behind model, the example used in the preceding section for the single-file page would be in two parts. The markup would be in one file (in this example, SamplePage.aspx) and would be similar to the single-file page, as shown in the following code example.

Examples of Visual C# ASP.NET Code-Behind Page Model

ASP.NET Code Input:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <title>Sample Page</title>
       <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
       <%@ Page Language="C#" CodeFile="aspnetpage_codemodel_01a_03.aspx.cs" Inherits="aspnetpage_codemodel_01a_03" AutoEventWireup="true" %>
    </head>
    <body>
       <form id="form1" runat="server">
           <h3>Code-Behind Page Model</h3>
           <div>

               <asp:Label id="Label1"
                   runat="server" Text="Label">
               </asp:Label>

           <br /><br />

               <asp:Button id="Button1"
                   Text="Button"
                   OnClick="Button1_Click"
                   runat="server">
               </asp:Button>
           </div>
       </form>
    </body>
</html>
ASP.NET cs Code Input:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class aspnetpage_codemodel_01a_03 : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Clicked at " + DateTime.Now.ToString();
    }
}
HTTP Response Output:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <title>Sample Page</title>
       <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    </head>
    <body>
       <form method="post" action="./aspnetpage_codemodel_01a_03.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTE5MDUzMTQ2MGRkUpj13BnizyfbJjrRxxmo5oRdN06fAeWn+QHj9soTWsE=" />
</div>

<div class="aspNetHidden">

	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="F6002D58" />
	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAIE30T1eOGv8/crhdxOVCmzzfg78Z8BXhXifTCAVkevdwDyMfg8Wyh7SA8JKSiVCSoSPzYHMF2KSNJqbwBoA5tx" />
</div>
           <h3>Code-Behind Page Model</h3>
           <div>

               <span id="Label1">Label</span>

           <br /><br />

               <input type="submit" name="Button1" value="Button" id="Button1" />
           </div>
       </form>
    </body>
</html>
ASPX Web Page Embedded Output:

Examples of Visual Basic ASP.NET Code-Behind Page Model

ASP.NET Code Input:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <title>Sample Page</title>
       <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
       <%@ Page Language="vb" CodeFile="aspnetpage_codemodel_01a_04.aspx.vb" Inherits="aspnetpage_codemodel_01a_04"  %>
    </head>
    <body>
       <form id="form1" runat="server">
           <h3>Code-Behind Page Model</h3>
           <div>
               <asp:Label id="Label1"
                   Text="Label"
                   runat="server">
               </asp:Label>
               <br /><br />
               <asp:Button id="Button1"
               Text="Button"
               OnClick="Button1_Click"
               runat="server">
               </asp:Button>
           </div>
       </form>
    </body>
</html>
ASP.NET vb Code Input:
Partial Class aspnetpage_codemodel_01a_04
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "Clicked at " & DateTime.Now.ToString()
    End Sub
End Class
HTTP Response Output:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <title>Sample Page</title>
       <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    </head>
    <body>
       <form method="post" action="./aspnetpage_codemodel_01a_04.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTE5MDUzMTQ2MGRkEc5UTLFtVmb2w/LY/OomMp06jLO5ieOcYTS8r13XeUM=" />
</div>

<div class="aspNetHidden">

	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="19CD5096" />
	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAJU/CgjWWrB1FeMv1DtIrOqzfg78Z8BXhXifTCAVkevd1bA1lH448S2JYitFH6LairLiVUQWMajUBoFNtr5HSW3" />
</div>
           <h3>Code-Behind Page Model</h3>
           <div>
               <span id="Label1">Label</span>
               <br /><br />
               <input type="submit" name="Button1" value="Button" id="Button1" />
           </div>
       </form>
    </body>
</html>
ASPX Web Page Embedded Output:
There are two differences in the .aspx page between the single-file and the code-behind models. In the code-behind model, there is no script block with the runat="server" attribute. (The page can contain script blocks without the runat="server" attribute if you want to write client-side script in the page.) The second difference is that the @ Page directive in the code-behind model contains attributes that reference an external file (SamplePage.aspx.vb or SamplePage.aspx.cs) and a class. These attributes link the .aspx page to its code.
The code is in a separate file. The following code example shows a code-behind file that contains the same Click event handler as the example for the single-file page.
The code-behind file contains the complete class declarations in the default namespace. However, the class is declared with the partial keyword, which indicates that the class is not contained entirely in one file. Instead, when the page runs, the compiler reads the .aspx page and the file it references in the @ Page directive, assembles them into a single class, and then compiles them as a unit into a single class.
The partial class file inherits from the page Page class. For more information, see ASP.NET Page Class Overview.

Choosing a Page Model

 The single-file and code-behind page models are functionally the same. At run time, the models execute the same way, and there is no performance difference between them. Choosing a page model therefore depends on other factors, such as how you want to organize the code in your application, whether it is important to separate page design from coding, and so on.

Note

 Examples you read in the documentation are often presented as single-file pages. This is primarily as a convenience for the reader, because it avoids having to illustrate two files for each example. The fact that you find single-file examples in the documentation should not be interpreted to mean that single-file pages are favored over code-behind pages or that there is some other inherent benefit to single-file pages.

Advantages of Single-File Pages

As a rule, the single-file model is suitable for pages in which the code consists primarily of event handlers for the controls on the page.
Advantages of the single-file page model include the following:
  • In pages where there is not very much code, the convenience of keeping the code and markup in the same file can outweigh other advantages of the code-behind model. For example, it can be easier to study a single-file page because you can see the code and the markup in one place.
  • Pages written using the single-file model are slightly easier to deploy or to send to another programmer because there is only one file.
  • Because there is no dependency between files, a single-file page is easier to rename.
  • Managing files in a source code control system is slightly easier, because the page is self-contained in a single file.

Advantages of Code-Behind Pages

 Code-behind pages offer advantages that make them suitable for Web applications with significant code or in which multiple developers are creating a Web site.
Advantages of the code-behind model include the following:
  • Code-behind pages offer a clean separation of the markup (user interface) and code. It is practical to have a designer working on the markup while a programmer writes code.
  • Code is not exposed to page designers or others who are working only with the page markup.
  • Code can be reused for multiple pages.
 

Compilation and Deployment

 Compilation and deployment of both single-file and code-behind pages is similar. At its simplest, you copy the page to the target server. If you are working with code-behind pages, you copy both the .aspx page and the code file. When the page is first requested, ASP.NET compiles the page and runs it. Note that in both scenarios you deploy source code with the markup.
Alternatively, you can precompile your Web site. In that case, ASP.NET produces object code for your pages that you can copy to the target server. Precompilation works for both single-file and code-behind models, and the output is the same for both models. For more information, see ASP.NET Precompilation Overview.

See Also

 

Concepts

 ASP.NET Page Life Cycle Overview
ASP.NET Compilation Overview

Sources and References

  • https://docs.microsoft.com/en-us/previous-versions/aspnet/ms178138(v=vs.100)
  • https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page?view=netframework-4.8
  • https://docs.microsoft.com/en-us/previous-versions/aspnet/015103yb(v=vs.100)

©sideway

ID: 211000010 Last Updated: 10/10/2021 Revision: 0 Ref:

close

References

  1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
  2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
  3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
  4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
  5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
  6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
  7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
  8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
  9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
  10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
  11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
  12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
  13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
  14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
  15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
  16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
  17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
  18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
  19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
  20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model
  22. ADO Fundamentals,  , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx
close

Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 339

Reference 79

Computer

Hardware 249

Software

Application 213

Digitization 32

Latex 52

Manim 205

KB 1

Numeric 19

Programming

Web 289

Unicode 504

HTML 66

CSS 65

SVG 46

ASP.NET 270

OS 429

DeskTop 7

Python 72

Knowledge

Mathematics

Formulas 8

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 34

Coordinate Geometry 2

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1


Copyright © 2000-2024 Sideway . All rights reserved Disclaimers last modified on 06 September 2019