views:

228

answers:

1

I'm accessing Masterpage properties from a regular ASP.NET C# page by doing the following:

((SecondMasterPage)(Page.Master)).speciallink = true;  
((SecondMasterPage)(Page.Master)).specialspan = false;

That works fine in a page's code-behind, but when I try to move it to a function in my base class file like this:

public class MyBaseClass : System.Web.UI.Page
{
public void portalFuncs()
{
    ((SecondMasterPage)(Page.Master)).speciallink = true;  
    ((SecondMasterPage)(Page.Master)).specialspan = false;
}
}

... I get the following error message:

Compiler Error Message: CS0246: The type or namespace name 'SecondMasterPage' could not be found (are you missing a using directive or an assembly reference?)

My base class file is in my App_Code directory and other functions in there work without errors. Why won't this function work? Also if a function like this particular one won't work in my base class file, where could I put it so it would be available to be called from any page?

A: 

Unless I'm missing something, you should just be able to go into the codebehind for your SecondMasterPage file, and check the namespace of it. Perhaps the namespace is incorrect or something different than you want.

In your base class, use a using my.masterpage.namespace; statement to get it to work.

womp
That seems like a strange way to do it (or at least a way I have never used) but I tried adding using SecondMasterPage.masterpage.namespace;This gave me the following error:Compiler Error Message: CS1041 Identifier expected, 'namespace' is a keyword.
itsatrp
No no... that was an example. Use the actual namespace from your masterpage class, not literally "SecondMasterPage.masterpage.namespace".
womp
The way I have built this website, I don't have a namespace declared in the Master Page code behind. I just have a class that looks like this:public partial class SecondMasterPage : MyBaseClass{.....}
itsatrp
Ah, right - I forgot that websites have no namespace declaration by default. It's all coming back now... you can't actually access a class outside of App_Code like this, even if they share a namespace. See this blog post for a way around it: http://blogs.msdn.com/asgoyal/archive/2009/06/16/consuming-a-class-which-is-outside-of-app-code-folder-in-an-asp-net-website-project.aspx
womp
Oh yes, I have been using the "Web Site" model as opposed to the "Web Application Project." I don't understand how that blog post is a solution though. My class file is in App_Code, not outside of it.
itsatrp