views:

613

answers:

2

Hi,

I am trying to create a general class, in which all my ASP.Net pages inherit from so I can share functions across multiple pages.

To do this I would create a new class which inherits from System.Web.UI.Page (the content pages need to inherit this), and then my content pages would inherit the newly create class.

My problem is that the Masterpage inherits from System.Web.UI.Masterpage.

How can I set up my project so both content pages and Masterpage and use functions from the general class?

Please don't hesitate to ask if I am unclear!

Thanks!

E

+1  A: 

First, not sure why you'd want to do this. By their function Master Pages should mostly have functions that your Pages shouldn't be concerned with and visa versa. And if you just need some common functionality that isn't page dependent you can just create a static class (much like Math) or a helper class of some kind that you can implement in MasterPage and Page custom base classes.

But your only real option is to create two custom base classes. One that inherits MasterPage and the other from Page. Both will need to implement an interface ICommon which you create. Then create another static class that you can proxy all the functions to.

Yucky solution but it's the only one I can think of.

EDIT

Here's a better solution

public class Helper
{
   public static int getUserID(...)
   {
      // ... Code to get User ID
   }
}

In your masterpages and pages use

int UserID = Helper.getUserID(...);
Spencer Ruport
I want to do this because I have many functions and data that I want to be able to use across web pages. Without this, I will have to create a function getUserID, on every page that I want to get a user ID. For Page specific functions, I will leave them in the CodeBehind file.
Erik Ahlswede
Since getUserID isn't page dependent you can just create a static class that they can both access. I'll edit my post.
Spencer Ruport
Perfect. Thanks
Erik Ahlswede
A: 

I don't think you can do this, the MasterPage inherits UserControl and Page inherits TemplateControl. Like Spencer said, I would just create a Helper/Utility class.

mxmissile