views:

234

answers:

4

All of the web forms / pages that my project extend on a derived class for the default page class.

I.e. instead of

public partial class myfirstpage:System.web.ui.page {}

i have public partial class myfirstpage:myderivedclass {}

However in the codebehind of the masterpage, if i do 'this.page' it assumes im still using system.web.ui.page.

Anyone know how i can change this my new derived class instead?

A: 

I would assume you need to override the page field of the System.Web.Ui.Page in your new class.

Robban
+6  A: 

In the master, create a new property:

public new MyDerivedPage Page { get { return (MyDerivedPage) base.Page; } }
Abraham Pinzur
This is pretty much how I did it also - worked very well.
Joe Enos
This assumes that the masterpage will always be used with the derived page class which may or may not be the case, it would be safer to add this to the derived page class.If you go with adding it to the MasterPage you should also add the MasterType page variable to your pages so you can use Master.Page and it will automatically cast the Master property to your Master Page and therefore return your overrided property, as in:<%@ MasterType VirtualPath="~/path to master" >
Adam Fox
Youre saying rather than calling the page functions from the master, put the functions in the master and call them from the page?
maxp
The master page should have common functions that the pages can use as they need to (they know what they are doing). This decouples your master from your derived page class. Just a suggestion.
Adam Fox
Further more, the master knows about layout and its containers, any shared logic should be in your derived class
Adam Fox
+1  A: 

I'm not sure what you mean by "it assumes I'm still using System.Web.UI.Page. Do you mean that the data type of this.Page is System.Web.UI.Page but you were expecting it to be MyDerivedClass?

If that's the case, then there's no way to change that data type. You can create your own property that "hides" the existing one, but you can't actually change it.

public MyDerivedClass Page
{
    get { return (MyDerivedClass)base.Page; }
}
Adam Robinson
A: 
var page = (myderivedclass) this.page;
mxmissile