如何从基类或其他函数文件访问母版页属性
我通过执行以下操作从常规 ASP.NET C# 页面访问母版页属性:
((SecondMasterPage)(Page.Master)).speciallink = true;
((SecondMasterPage)(Page.Master)).specialspan = false;
这在页面的代码隐藏中工作正常,但是当我尝试将其移动到基类文件中的函数时,如下所示:
public class MyBaseClass : System.Web.UI.Page
{
public void portalFuncs()
{
((SecondMasterPage)(Page.Master)).speciallink = true;
((SecondMasterPage)(Page.Master)).specialspan = false;
}
}
...我收到以下错误消息:
编译器错误消息:CS0246:找不到类型或命名空间名称“SecondMasterPage”(是否缺少 using 指令或程序集引用?)
我的基类文件位于我的 App_Code 目录和其他函数中在那里工作没有错误。为什么这个功能不起作用?另外,如果像这样的特定函数在我的基类文件中不起作用,我可以将它放在哪里以便可以从任何页面调用它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非我遗漏了某些内容,否则您应该能够进入 SecondMasterPage 文件的代码隐藏,并检查它的命名空间。也许命名空间不正确或者与您想要的不同。
在您的基类中,使用
using my.masterpage.namespace;
语句使其正常工作。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.