views:

497

answers:

2

I have a master page in a asp.net project, which provides a method that I would like to call in derived classes through an helper function, so I tried to create a base class for my pages:

// the master page 
public partial class TheMasterPage : MasterPage {
    public string TheMethod(string s1) {
        // ...
    }
}
// base class providing an helper method
public class HelperPage : Page {
    protected bool HelperMethod() {
        string value = ((TheMasterPage)this.Master).TheMethod("some value");
        return (value == "something");
    }
}
// derived class
public partial class Page1 : HelperPage {
    protected void Page_Load(object sender,EventArgs e) {
        if (HelperMethod()) {
            // ...
        }
    }
}

but if I try to do this, I get an error saying "the type or namespace HelperPage could not be found".
Is there a way to do what I'm trying to do without moving the method in the master page to the App_Code folder?
In general, is it possible to reference from the asp.net application another type defined in the application itself?

EDIT:
@John Rasch

The way I've seen this done is by storing the base pages in a different assembly. That way, all you have to do is add a reference to that assembly and you can inherit from HelperPage type.

It would then be enough to move everything to the App_Code folder; the problem is that in that way the HelperPage would not be able to access the MasterPage, unless I also create a base class for the master page under App_Code or in the new assembly... it's probably the most sensible solution, but I was wandering if there's a way to avoid that - and anyway I cannot understand why you cannot access a type declared in the application...

+1  A: 

The way I've seen this done is by storing the base pages in a different assembly. That way, all you have to do is add a reference to that assembly and you can inherit from HelperPage type.

Edit:

I cannot understand why you cannot access a type declared in the application...

The reason is because the pages are compiled into more than 1 assembly. If you look at the folder you're publishing to, you'll see a bunch of .dlls with names like App_Web_xxxxxxxx.dll.

From MSDN:

By default, the compiler works in "batch mode," in which the output of multiple source files is compiled into single assemblies according to the type of file, file dependencies, and other criteria. The result is a target site containing a set of assemblies with the executable code for the original source files.

In some instances, the assemblies created with batch compilation may not be ideal for deploying a Web site to a production server. The assembly names are generated automatically by the compiler and it is therefore not obvious which assemblies map to which source files. The compiler also creates new names each time it runs, so that the names of assemblies might not be the same after each compilation. In addition, if source files have changed, the compiler might batch up source files differently, meaning that the resulting assemblies do not necessarily represent the same source files. If you are maintaining a deployed Web site and want to update only the assemblies for recent changes, the output from batch compilation can make that job more complicated.

I'm guessing you could use the merge tool on the MSDN site to ensure that the whole application resides in a single assembly, but that seems a bit over-the-top. I would just stick with using the App_Code folder for simplicity.

John Rasch
Thanks! that's exactly what I wanted to know, but I couldn't find any reference.
Paolo Tedesco
A: 

You need to specify the fully qualified name of the HelperPage. You can usually determine this just by looking at the namespace {} section of the code just prior to the class definition. I think it will also show you if you hover over your class name.

Spencer Ruport
Actually this doesn't work for me... even if I add a namespace around the class definition and reference it with the fully qualified name I still get the same error.
Paolo Tedesco
Is it in the same assembly? You may have to add a reference. Also take a look at the object explorer and see if you can find the object in there. That may clue you into what is going on.Also if you can take snapshots of your solution explorer an class view listings that show both the page that is trying to inheirit from the class and the class itself would be helpful.
Spencer Ruport