如何从基类或其他函数文件访问母版页属性

发布于 2024-08-21 05:40:08 字数 659 浏览 3 评论 0原文

我通过执行以下操作从常规 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

围归者 2024-08-28 05:40:08

除非我遗漏了某些内容,否则您应该能够进入 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文