maxp
maxp

Reputation: 25171

Define derived class in Masterpage (c#)

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?

Upvotes: 1

Views: 614

Answers (4)

mxmissile
mxmissile

Reputation: 11681

var page = (myderivedclass) this.page;

Upvotes: 0

Adam Robinson
Adam Robinson

Reputation: 185703

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; }
}

Upvotes: 1

Av Pinzur
Av Pinzur

Reputation: 2228

In the master, create a new property:

public new MyDerivedPage Page { get { return (MyDerivedPage) base.Page; } }

Upvotes: 6

Robban
Robban

Reputation: 6802

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

Upvotes: 0

Related Questions