Share via


How to: Migrate an ASP.NET 1.x Stand-Alone Class that Accesses Another Control's Code-Behind Class

This topic demonstrates how to migrate an ASP.NET version 1.x stand-alone class to ASP.NET version 2.0. The stand-alone class example used in the following procedures accesses a static field in another control's code-behind class.

In previous versions of ASP.NET, all pages in a Web application are compiled into a single assembly so that references from the stand-alone class to the code-behind class can be resolved. In ASP.NET 2.0, the page compilation, by default, results in multiple assemblies; references from the stand-alone class to the code-behind class will result in a compilation error. This topic demonstrates a coding pattern to enable a Web application that contains stand-alone classes that reference code-behind classes to successfully compile and run. The pattern uses an abstract base stub class in the App_Code directory.

The procedures in this topic require the .NET Framework version 2.0.

To access a code-behind class using the ASP.NET 1.x coding pattern

  1. In the root of your Web application, create a folder called Controls. In this folder, create a user control with a code-behind class, as shown in the following example.

    <%@ Control Language="VB" AutoEventWireup="false"
        CodeBehind="CodeBehindExampleUserControl.ascx.cs" 
        Inherits="CodeBehindExampleUserControl" %>
    
    <%@ Control Language="C#" AutoEventWireup="false" 
        CodeBehind="CodeBehindExampleUserControl.ascx.cs" 
        Inherits="CodeBehindExampleUserControl" %>
    
    Public Class CodeBehindExampleUserControl
        Inherits System.Web.UI.UserControl
    
        Public Shared Name As String = "CodeBehindExampleUserControl"
    
        Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load
    
            ' User control code.
    
        End Sub
    End Class
    
    public class CodeBehindExampleUserControl :
        System.Web.UI.UserControl
    {
        public static string Name = "CodeBehindExampleUserControl";
    
        protected void Page_Load(object sender,
            System.EventArgs e)
        {
            // User control code.
        }
    }
    

    Note

    In this example, you are using a class in the control's code-behind file and the CodeBehind attribute in the @ Page directive in the .ascx file. In the next procedure, you will use a partial class in the code-behind class and replace the CodeBehind attribute with the Codefile attribute.

  2. In the root of your Web application, create a stand-alone class as shown in the following example.

    Imports Microsoft.VisualBasic
    
    Public Class StandAlone
    
        Private _name As String = "StandAlone"
    
        Public ReadOnly Property Name() As String
            Get
                Return _name & " + " & CodeBehindExampleUserControl.Name
            End Get
        End Property
    
    End Class
    
    public class StandAlone
    {
        private string _name;
    
        public StandAlone()
        {
            // constructor
            _name = "StandAlone";
        }
        public string Name
        {
            get
            {
                return _name + " + " + CodeBehindExampleUserControl.Name;
            }
        }
    }
    
  3. In the root of your Web application, create a Web page with a code-behind class as shown in the following example.

    <%@ Page Language="VB" AutoEventWireup="false" 
        CodeBehind="CodeBehindExample.aspx.vb" 
        Inherits="CodeBehindExample" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Code-Behind Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
          <asp:Label id="Label1" runat="server"></asp:Label>    
        </div>
        </form>
    </body>
    </html>
    
    <%@ Page Language="C#" AutoEventWireup="true" 
        CodeBehind="CodeBehindExample.aspx.cs" 
        Inherits="CodeBehindExample" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Code-Behind Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
          <asp:Label id="Label1" runat="server"></asp:Label>    
        </div>
        </form>
    </body>
    </html>
    
    Public Class CodeBehindExample
        Inherits System.Web.UI.Page
    
        Protected Label1 As System.Web.UI.WebControls.Label
    
        Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load
    
            Dim c As New StandAlone()
            Label1.Text = c.Name
    
        End Sub
    End Class
    
    public class CodeBehindExample :
        System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label Label1;
    
        protected void Page_Load(object sender,
            System.EventArgs e)
        {
            StandAlone c = new StandAlone();
            Label1.Text = c.Name;
    
        }
    }
    

    Note

    In this example, you are using a class in the page's code-behind file and the CodeBehind attribute of the @ Page directive in the .aspx file. In the next procedure, you will use a partial class in the code-behind class and replace the CodeBehind attribute with the Codefile attribute.

  4. Create a Bin folder in your Web application if one does not already exist, and compile the Web page, user control, and stand-alone class into an assembly as shown in the following example.

    vbc /target:library /nologo /out:bin\CodeBehindExampleVB.dll CodeBehindExample.aspx.vb controls\CodeBehindExampleUserControl.ascx.vb StandAlone.vb
    
    csc /target:library /nologo /out:bin\CodeBehindExampleCS.dll CodeBehindExample.aspx.cs controls\CodeBehindExampleUserControl.ascx.cs StandAlone.cs
    
  5. Request the Web page CodeBehindExample.aspx in a browser to verify that it works in the .NET Framework 2.0.

    The page should return a concatenated string, "StandAlone + CodeBehindExampleUserControl". The CodeBehindExample page creates an instance of a new StandAlone class. The StandAlone class in turn accesses the Name field of the CodeBehindExampleUserControl user control.

To access a code-behind class using the ASP.NET 2.0 coding pattern

  1. Remove the assembly created in the previous procedure.

  2. In the user control created in the previous procedure, make changes so that the user control resembles the following example.

    <%@ Control Language="VB" AutoEventWireup="false" 
                CodeFile="CodeBehindExampleUserControl.ascx.vb" 
                Inherits="CodeBehindExampleUserControl_Migrated" %>
    
    <%@ Control Language="C#" AutoEventWireup="true" 
        CodeFile="CodeBehindExampleUserControl.ascx.cs" 
        Inherits="CodeBehindExampleUserControl_Migrated" %>
    
    Partial Class CodeBehindExampleUserControl_Migrated
        Inherits CodeBehindExampleUserControl
    
        Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load
    
            ' User control code.
    
        End Sub
    End Class
    
    public partial class CodeBehindExampleUserControl_Migrated :
        CodeBehindExampleUserControl
    {
        protected void Page_Load(object sender,
            System.EventArgs e)
        {
            // User control code.
        }
    }
    

    You are using a partial class in the code-behind file and the CodeFile attribute of the @ Page directive in the .ascx file. Additionally, the code-behind partial class inherits from a class called CodeBehindExampleUserControl and not the UserControl class as in the previous procedure. The definition of the CodeBehindExampleUserControl class is described in a subsequent step of this procedure. Finally, note that there is no definition for the static field Name; this is defined in the CodeBehindExampleUserControl class.

  3. Modify the stand-alone class to resemble the following example.

    Imports Microsoft.VisualBasic
    
    Public Class StandAlone
    
        Private _name As String = "StandAlone"
    
        Public ReadOnly Property Name() As String
            Get
                Return _name & " + " & CodeBehindExampleUserControl.Name
            End Get
        End Property
    
    End Class
    
    public class StandAlone
    {
        private string _name;
    
        public StandAlone()
        {
            // constructor
            _name = "StandAlone";
        }
        public string Name
        {
            get
            {
                return _name + " + " + CodeBehindExampleUserControl.Name;
            }
        }
    }
    
  4. Create an App_Code folder in your Web application if one does not exist. Move the stand-alone StandAlone class definition file to the App_Code folder.

  5. Inside the App_Code folder, create an abstract stub file for the user control. The stub file will define the CodeBehindExampleUserControl class and should resemble the following example.

    Imports Microsoft.VisualBasic
    
    Public MustInherit Class CodeBehindExampleUserControl
        Inherits System.Web.UI.UserControl
    
        Public Shared Name As String = "CodeBehindExampleUserControl"
    
    End Class
    
    abstract public class CodeBehindExampleUserControl : System.Web.UI.UserControl
    {
        public static string Name = "CodeBehindExampleUserControl";
    }
    

    The abstract class definition contains a definition of the static field Name. Because the CodeBehindExampleUserControl class is in the App_Code folder, its members and fields are visible to controls in your Web application.

  6. Request the Web page CodeBehindExample.aspx in a browser to verify that the page maintains the functionality it had when it used the ASP.NET 1.x coding pattern.

    In the ASP.NET 2.0 coding pattern, you do not need to explicitly compile the classes into an assembly.

See Also

Tasks

How to: Migrate an ASP.NET 1.1 Web Page Using the CodeBehind Attribute to ASP.NET 2.0