ASP.NET

Coding Options

In addition to supporting inline code (that is, including executable code directly inside a server-side script block), ASP.NET 2.0 offers two other distinct options for managing code: ASP.NET 1.x code behind, and ASP.NET 2.0 code beside. ASP.NET supports code behind for backwards compatibility. Code beside is the style employed by Visual Studio 2005. Let's look at these.

ASP.NET 1.x Style

ASP.NET 2.0 continues to support ASP.NET 1.1 style code behind. Using the code-behind directives in the ASPX file, you provide the code to run behind the page in a separate class and use the Page directive to tell ASP.NET which class to apply to the page. Then you tell ASP.NET the name of the file containing the source code for the class.

using System.Web;
public class HelloWorld4Code : System.Web.UI.Page
{
public void ShowLineage()
   {
        Response.Write("Check out the family tree: <br> <br>");
        Response.Write(this.GetType().ToString());
        Response.Write(" which derives from: <br> ");
        Response.Write(this.GetType().BaseType.ToString());
        Response.Write(" which derives from: <br> ");
        Response.Write(this.GetType().BaseType.BaseType.ToString());
        Response.Write(" which derives from: <br> ");
        Response.Write(
          this.GetType().BaseType.BaseType.BaseType.ToString());
        Response.Write(" which derives from: <br> ");
        Response.Write(
          this.GetType().BaseType.BaseType.BaseType.BaseType.ToString());
    }
}
<%@ Page Language="C#" Inherits="HelloWorld4Code"
    src="HelloWorld4Code.cs" Debug="true" %>
<html>
   <body>
       <h1>Hello World!!!</h1>
       <%
          // This block will execute in the Render_Control method.
          this.ShowLineage();
       %>
   </body>
</html>

With the ASP.NET 1.x style of code behind, ASP.NET sees the src keyword in the directives and compiles that file. ASP.NET reads the inherits keyword to figure out how to base the class that runs the page. In the example above, ASP.NET uses the HelloWorld4Code class to drive the page.

By using the SRC directive, you tell the ASP.NET runtime to compile the file named by the SRC directive. The ASP.NET runtime will compile it into the temporary directory. Alternatively, you may also precompile the file into an assembly containing the HelloWorld4Code class. For this to work, the precompiled assembly must appear in the bin directory of your virtual directory. If you precompile the page class and put the assembly in the bin directory, you don't even need to mention the source code file. In the absence of a SRC directive, the ASP.NET runtime will search the assemblies in the bin directory looking for the class specified in the INHERITS directive.

ASP.NET 2.x Style

The other coding option for ASP.NET is new for version 2.0. This model is sometimes referred to as code beside.

<%@ Page Language="C#" CodeFile="HelloWorld5code.cs"
    Inherits="HelloWorld5Code" %>
<html>
   <body>
       <h1>Hello World!!!</h1>
       <%
          // This block will execute in the Render_Control method
          ShowLineage();
       %>
   </body>
</html>

using System.Web;
public partial class HelloWorld5Code : System.Web.UI.Page
{
    public void ShowLineage()
    {
        Response.Write("Check out the family tree: <br> <br>");
        Response.Write(this.GetType().ToString());
        Response.Write(" which derives from: <br> ");
        Response.Write(this.GetType().BaseType.ToString());
        Response.Write(" which derives from: <br> ");
        Response.Write(this.GetType().BaseType.BaseType.ToString());
        Response.Write(" which derives from: <br> ");
        Response.Write(
        this.GetType().BaseType.BaseType.BaseType.ToString());
        Response.Write(" which derives from: <br> ");
        Response.Write(
        this.GetType().BaseType.BaseType.BaseType.BaseType.ToString());
    }
}

In this case, ASP.NET looks to the CodeFile directive to figure out what code to compile. ASP.NET expects to find a partial class to implement the page's logic. Partial classes let you split the definition of a type (class, struct, or interface) between multiple source files, with a portion of the class definition living in each file. Compiling the source code files generates the entire class. This is especially useful when working with generated code, such as that generated by Visual Studio. You can augment a class without going back and changing the original code. Visual Studio.NET 2005 prefers the code-beside/partial class code representation.

The following short listings, Listing 2-1 and Listing 2-2, show two files that implement a singular class named SplitMe.

Listing 2-1 Partial1.cs

using System;
// partial1.cs
using System;

public partial class SplitMe
{
   public static void Main()
   {
      SplitMe splitMe = new SplitMe();
      splitMe.Method1();
      splitMe.Method2();
   }

   public void Method2()
   {
      Console.WriteLine("SplitMe Method2");
   }
}

Listing 2-2 Partial2.cs

// Partial2.cs
using System;

public partial class SplitMe
{
   public static void Main()
   {
      SplitMe splitMe = new SplitMe();
      splitMe.Method1();
      splitMe.Method2();
   }

   public void Method2()
   {
      Console.WriteLine("SplitMe Method2");
   }
}

To compile the previous example, you may use the following command line:

csc /t:exe partial1.cs partial2.cs

This will generate an executable file named Partial2.exe.

After working with ASP.NET source code in the raw, it's time to look at how Visual Studio and ASP.NET work together. Visual Studio.NET 2005 brings many new features for creating and developing Web applications.

by BrainBellupdated
Advertisement: