ASP.NET Web Forms

Compiling ASP.NET Web Forms

As mentioned in Chapter 2, "The Common Language Runtime," all source code on an ASP.NET Web form is dynamically compiled into an intermediate language and then compiled just-in-time (JIT) into native machine code. This is handled completely behind the scenes; you do not need to compile a Web form manually. Web forms are compiled for several reasons:

  • Compiling the code in this way dramatically reduces the server's response time and processor load. Because the server spends less time processing each request, it can process more requests. Therefore, by using the Microsoft.NET framework, you can get more "bang for the buck" from your Web servers.

  • You can use strongly typed languages such as C#, Visual Basic.NET, JScript.NET, and managed C++ code for Web development on a Web form itself. This forces you to use proper coding techniques and helps eliminate the use of unstructured code that mars many standard Active Server Pages (ASP) applications.

  • The page compiler finds many errors that might be overlooked until runtime in an interpreted language such as VBScript in ASP.

The Intermediate Language (IL) that is generated in ASP.NET is human readable. You can learn a lot about the page processor by making changes to a page and seeing how the IL is affected.

Not just the server-side code is compiled into the IL; every element of the page is compiled, including the Hypertext Markup Language (HTML). HTML is placed into a text literal control and then added to the controls collection for the page.

Consider the simple Web form example in Listing 3.1. This page displays a message, using a label Web control. The text of the label control is specified on the server side. Listing 3.2 shows the IL code generated from the code in Listing 3.1. A complete discussion of the conversion of a Web form to IL is beyond the scope of this book. However, as you can see from Listing 3.1, even a very simple Web form can translate into some relatively intimidating code.

Listing 3.1 A Very Simple Web Form

<HTML>

<HEAD>
  <script language="C#" runat="server" >
   void Page_Load(Object Source, EventArgs E)
   {
     msg.Text = "Hello World!";
   }
  </script>
</HEAD>
<BODY>
  <asp:Label id=msg runat="server"></asp:Label>
  <%myerror%>
</BODY>
</HTML>

Listing 3.2 Automatically Generated Intermediate Language Code

namespace ASP {
   using System;
   using System.Collections;
   using System.Collections.Specialized;
   using System.Configuration;
   using System.Text;
   using System.Text.RegularExpressions;
   using System.Web;
   using System.Web.Caching;
   using System.Web.SessionState;
   using System.Web.Security;
   using System.Web.UI;
   using System.Web.UI.WebControls;
   using System.Web.UI.HtmlControls;


   public class myPage_aspx : System.Web.UI.Page, 
        System.Web.SessionState.IRequiresSessionState {
     
     private static System.Web.UI.AutomaticHandlerMethodInfos
                      __autoHandlers;
     
     
     #line 12 "c:\inetpub\wwwroot\Book\mypage.aspx"
     protected System.Web.UI.WebControls.Label msg; 

     #line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"
     
     private static bool __intialized = false;
     
     private static System.Collections.ArrayList
                __fileDependencies;
     
     
     #line 4 "c:\inetpub\wwwroot\Book\mypage.aspx"
     
    void Page_Load(Object Source, EventArgs E)
    {
     msg.Text = "Hello World!";
    }
   
     #line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"
     
     public myPage_aspx() {
       if ((ASP.myPage_aspx.__intialized == false)) {
         System.Collections.ArrayList dependencies = new
                    System.Collections.ArrayList();
         dependencies.Add
                 ("c:\\inetpub\\wwwroot\\Book\\mypage.aspx");
         ASP.myPage_aspx.__fileDependencies = dependencies;
         ASP.myPage_aspx.__intialized = true; 
       }
     }
     
     protected override 
          System.Web.UI.AutomaticHandlerMethodInfos AutoHandlers {
       get {
         return ASP.myPage_aspx.__autoHandlers;
       }
       set {
         ASP.myPage_aspx.__autoHandlers = value;
       }
     }
     
     protected System.Web.HttpApplication ApplicationInstance {
       get {
         return ((System.Web.HttpApplication)
                (this.Context.ApplicationInstance));
       }
     }
     
     public override string TemplateSourceDirectory {
       get {
         return "/Book";
       }
     }
     
     public override void InstantiateIn(System.Web.UI.Control
                         control) {
       this.__BuildControlTree(control);
     }
     
     private System.Web.UI.Control __BuildControlmsg() {
       System.Web.UI.WebControls.Label __ctrl;

       #line 12 "c:\inetpub\wwwroot\Book\mypage.aspx"
       __ctrl = new System.Web.UI.WebControls.Label();
       
       #line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"
       msg = __ctrl; 
       
       #line 12 "c:\inetpub\wwwroot\Book\mypage.aspx"
       __ctrl.ID = "msg";
       
       #line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"
       return __ctrl; 
     }
    
    private void __BuildControlTree(System.Web.UI.Control
                              __ctrl) {
      
      #line 1 "c:\inetpub\wwwroot\Book\mypage.aspx"
      this.__BuildControlmsg();
      
      #line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"

      #line 1 "c:\inetpub\wwwroot\Book\mypage.aspx"
      ((System.Web.UI.IParserAccessor)
               (__ctrl)).AddParsedSubObject(this.msg);

      #line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"
      __ctrl.SetRenderMethodDelegate(new 
             System.Web.UI.RenderMethod(this.__Render__control1));
    }

    private void __Render__control1
           (System.Web.UI.HtmlTextWriter __output, 
            System.Web.UI.Control parameterContainer) {
      System.Web.UI.Control Container;
      Container = parameterContainer;
      __output.Write("<HTML>\r\n\r\n<HEAD>\r\n\t");
      __output.Write("\r\n</HEAD>\r\n<BODY>\r\n  ");
      
      #line 12 "c:\inetpub\wwwroot\Book\mypage.aspx"
      parameterContainer.Controls[0].RenderControl(__output);
      
      #line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"
      __output.Write("\r\n  ");
      
      #line 13 "c:\inetpub\wwwroot\Book\mypage.aspx"
      myerror

      #line 1000000 "c:\inetpub\wwwroot\Book\mypage.aspx"
      __output.Write("\r\n</BODY>\r\n</HTML>");
    }
    
    protected override void FrameworkInitialize() {
      this.FileDependencies = 
               ASP.myPage_aspx.__fileDependencies;
      this.EnableViewStateMac = true;
    }
    
    public override int GetTypeHashCode() {
      return -1269963154;
    }
  }
}

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only 3 numbers of interest to a computer scientist: 1, 0 and infinity”