OdeToCode IC Logo

Migrating Base Page Classes Currently Broken

Thursday, July 7, 2005

If you derive your web form code-behind pages from a base class to manipulate common controls, your code won’t be happy with beta 2. For instance, deriving a web form from the following class will result in a null reference exception in AppBasePage_Load (commonLabel is null).

using System;

using System.Web.UI;

using System.Web.UI.WebControls;


public class AppBasePage : Page


    protected override void OnInit(EventArgs e)


        this.Load += new EventHandler(AppBasePage_Load);



    void AppBasePage_Load(object sender, EventArgs e)


        commonLabel.Text = "Hello World";



    protected Label commonLabel;


In 1.x ASP.NET would wire up commonLabel to a server-side control with the same ID. In 2.0 ASP.NET will code-gen a partial class with a commonLabel declaration that hides the base class declaration. The base class object's commonLabel reference will remain null.

According to ScottGu’s “Compatibility Testing” post, this problem should be fixed by the migration wizard in RTM bits. Hopefully the fix will be in a CTP soon  - I'm curious to see if there will be an elegant solution.