Can I still use base page classes in Whidbey?

Tonight as I was experimenting with Beta 2 I ran into some troubles with my pages that inherit from a base page class.  All the controls that we’re being setup in the base page class were throwing NullReference exceptions.

MyPage.aspx  inherits MyPage inherits MyBasePage

MyPage.aspx and MyPage.aspx.cs don’t need controls defined since they are partial classes and have the magic that goes along with being setup as such.  The MyBasePage class however isn’t a partial class since it lives in a completely different location (different assembly).  In that base page class I have protected Button saveButton defined, but it’s always null.  What’s the magic step that’s necessary to be able to get a reference to the saveButton from within my base page class?

UPDATE: Well I was able to retrieve a reference to the controls by doing a this.FindControl within the Init of the page.  This isn’t a terrible change, but, I am wondering if it’s the answer to how to support this.  To provide a little more context.

In 1.1 I had a base page EditPage.cs with the following declaration:

protected System.Web.UI.WebControls.Button saveButton;

protected virtual void InitializeComponent() {
   this.saveButton.Click += new System.EventHandler(this.saveButton_Click);
}

When I run this in .NET 2.0 I get a NullReferenceException, unless I do something like:

protected System.Web.UI.WebControls.Button saveButton;

protected virtual void InitializeComponent() {
   this.saveButton = (Button) this.FindControl(“saveButton”);
   this.saveButton.Click += new System.EventHandler(this.saveButton_Click);
}

So what’s the scoop, is this the only way to support this or am I missing some super duper special keyword, attribute, or magic wave of the hand to get this to work?

UPDATE #2:

Source from my test is available here.  I get a warning when I compile (below) and when the MyBasePage tries to setup the Click event handler a NullReferenceException is thrown.

“Warning 1 '_Default.myButton' hides inherited member 'MyBasePage.myButton'. Use the new keyword if hiding was intended. C:\Inetpub\wwwroot\AspNetv2\Default.aspx 1 1 C:\...\AspNetv2\”


Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml" >
<
head runat="server">
<title>Untitled Page</title>
</
head>
<
body>
<form id="form1" runat="server">
<div>
<asp:Button ID="myButton" runat="server" Text="My Button"/>
</div>
</form>
</
body>
</
html>


Default.aspx.cs
public partial class _Default : MyBasePage {
   protected void Page_Load(object sender, EventArgs e) {
   }
}

MyBasePage.cs
public class MyBasePage : System.Web.UI.Page {
   public System.Web.UI.WebControls.Button myButton;
  
   protected override void OnInit(EventArgs e) {
      myButton.Click += new EventHandler(myButton_Click);
      base.OnInit(e);
   }

   void myButton_Click(object sender, EventArgs e) {
      Response.Write("Button Clicked");
   }
}


 

# re: Can I still use base page classes in Whidbey?

Saturday, April 16, 2005 2:05 PM by scotgu    
Hi Steve,

If you can send me a simple repro sample that I can take a look at. My email address is: scottgu@microsoft.com.

BTW -- can you also try changing the field declaration of &quot;saveButton&quot; to be public instead of protected and see if that makes a difference?

Thanks,

Scott

# re: Can I still use base page classes in Whidbey?

Saturday, April 16, 2005 4:47 PM by Tim Haines    
Good to see you're listening Scott. ;-)

# re: Can I still use base page classes in Whidbey?

Sunday, April 17, 2005 3:05 AM by Sean Chase    
This ought to be very interesting. I ran the aspnet_compiler.exe command line utility against the code you posted. Then I ran Reflector against the generated assemblies and I see tha the base class defines a public myButton while at the same time, so does the page (albeit protected). Not to overstate the obvious, but that's the problem. In 1.1 you would have to move the protected field declaration for the button from the page's code-behind into the base class. But, I'm not sure that you can edit the auto-generated partial class??? Anyway, here's what the reflector code looks like just in case you are interested. I'll look forward to Scott's response to your issue...

public class MyBasePage : Page
{
// Methods
public MyBasePage();
private void myButton_Click(object sender, EventArgs e);
protected override void OnInit(EventArgs e);

// Fields
public Button myButton;
}


public class Test_aspx : MyBasePage, IRequiresSessionState
{
// Methods
public Test_aspx();
protected void myButton_Click(object sender, EventArgs e);
protected void Page_Load(object sender, EventArgs e);

// Properties
protected HttpApplication ApplicationInstance { get; }
public override string AppRelativeTemplateSourceDirectory { get; set; }
protected DefaultProfile Profile { get; }

// Fields
protected HtmlForm form1;
protected Button myButton;
}

# re: Can I still use base page classes in Whidbey?

Sunday, April 17, 2005 4:30 AM by Steve    
Thanks Sean, I didn't even think to look at what was happening with reflector. I forwarded the code snippet on to Scott as well so we'll see what he says.

# re: Can I still use base page classes in Whidbey?

Tuesday, April 26, 2005 1:59 AM by Max Metral    
This is killing us too at the moment. ASP.Net should be reflecting to inherited base classes when determining whether to define the control or not, in my opinion. I'm pretty sure VS2003 did this.

# re: Can I still use base page classes in Whidbey?

Tuesday, April 26, 2005 2:00 AM by Steve    
Yeah, VS.NET 2003 did do this. I'm still not 100% clear why it couldn't also be done in Whidbey.

# re: Can I still use base page classes in Whidbey?

Wednesday, April 27, 2005 9:20 AM by Sean Chase    
Although this issue doesn't affect anything that I'm doing at the moment, I'm definitely interested in hearing what the official MS response is.

# An Update on Base Page Classes in Whidbey from Scott G

Wednesday, May 11, 2005 11:05 PM by Steve Eichert    
An Update on Base Page Classes in Whidbey from Scott G

# re: Can I still use base page classes in Whidbey?

Friday, July 21, 2006 12:00 AM by Ken Jacobs    
Try adding a CodeFileBaseClass attribute containing the fully qualified name of your base page class to your @Page directive. This seems to stop the compiler from creating new field definitions when it generates the partial class.

# re: Can I still use base page classes in Whidbey?

Monday, August 07, 2006 9:01 PM by djeeg    
you should try using the CodeFileBaseClass attribute on the page directive.

Post a Comment

 
 
Prove you're not a spammer: 
8 + 7 =