如何从内容页使用母版页中的方法

发布于 2024-10-19 00:49:39 字数 557 浏览 2 评论 0原文

我正在使用 C# 编写 ASP.NET 4 应用程序。我有一个母版页,其中有以下方法:

public void DisplayMessage(string input)
{
   Label myMessageDisplayer = (Label)FindControl("uxMessageDisplayer");
   myMessageDisplayer.Text = input;
}

我可以从内容页调用此方法吗?
目前,我在内容页面中使用此代码:

Master.DisplayMessage("Item has been inserted.");

并且收到此错误:

“System.Web.UI.MasterPage”不包含“DisplayMessage”的定义,并且找不到接受“System.Web.UI.MasterPage”类型的第一个参数的扩展方法“DisplayMessage”(您是否缺少using 指令还是程序集引用?)

任何帮助将不胜感激。

I am writing an ASP.NET 4 application with C#. I have a Master Page, inside of which I have the following method:

public void DisplayMessage(string input)
{
   Label myMessageDisplayer = (Label)FindControl("uxMessageDisplayer");
   myMessageDisplayer.Text = input;
}

Can I call this method from a content page?
At the moment, I use in Content Page this code:

Master.DisplayMessage("Item has been inserted.");

And I receive this error:

'System.Web.UI.MasterPage' does not contain a definition for 'DisplayMessage' and no extension method 'DisplayMessage' accepting a first argument of type 'System.Web.UI.MasterPage' could be found (are you missing a using directive or an assembly reference?)

Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

前事休说 2024-10-26 00:49:39

您可以使用转换来获取母版页类型,如其他人所示,或者您可以添加 MasterType 指令添加到您的页面标记(位于顶部,标准 <%@ Page %> 指令所在的位置):

<%@ MasterType TypeName="YourNamespace.YourMasterPageType" %>

然后在您的页面中代码,您可以使用示例中的内容:

Master.DisplayMessage("Item has been inserted.");

MasterType 指令可从 .NET 2 向上使用。

You can use casting to get your master page type, as others have shown, or you can add the MasterType directive to your page markup (at the top, where the standard <%@ Page %> directive is):

<%@ MasterType TypeName="YourNamespace.YourMasterPageType" %>

Then in your page code, you can have just what you have in your example:

Master.DisplayMessage("Item has been inserted.");

The MasterType directive is available from .NET 2 upwards.

下雨或天晴 2024-10-26 00:49:39

您需要将 Master 转换为实际的 Masterpage 类型

// MyMasterPage is the type of your masterpage
((MyMasterPage)Master).DisplayMessage("Item has been inserted.");

您还可以添加 MasterType 指令添加到内容页面顶部以实现相同的效果:

<%@ MasterType VirtualPath="~/MyMasterPage.master" %>

然后您可以使用:

Master.DisplayMessage('Item has been inserted.');

You need to cast the Master to the actual Masterpage type

// MyMasterPage is the type of your masterpage
((MyMasterPage)Master).DisplayMessage("Item has been inserted.");

You can also add a MasterType directive to the top of your content page to achieve the same thing:

<%@ MasterType VirtualPath="~/MyMasterPage.master" %>

Then you can use:

Master.DisplayMessage('Item has been inserted.');
路还长,别太狂 2024-10-26 00:49:39

我做了/尝试这个:

我的控件(MenuEsquerdo.ascx)

<%--<%@ MasterType VirtualPath="~/Site.Master" %>--%>
<%--<%@  MasterPageFile="~/Site.Master" %>--%>
<%--<%@ MasterType TypeName="Delivery.SiteMaster" %>--%>
<%--<%@ MasterType VirtualPath="~/Site.Master" %>--%>
<%@ MasterType TypeName="Delivery.SiteMaster" %>

我的设计器控件(MenuEsquerdo.ascx.designer.cs)

public new Delivery.SiteMaster Master {
            get {
                return ((Delivery.SiteMaster)(base.Master)); // 'System.Web.UI.UserControl' does not contain a definition for 'Master'
            }
        }

和我的代码(MenuEsquerdo.ascx.cs)

RepeaterListaMenuEsquerdoCtr.DataSource = this.Master.ClassMercadorias.Take(20);
RepeaterListaMenuEsquerdoCtr.DataBind();

知道,我如何解决这个问题:'System. Web.UI.UserControl'不包含'Master'的定义?

I made/try this:

My Control (MenuEsquerdo.ascx)

<%--<%@ MasterType VirtualPath="~/Site.Master" %>--%>
<%--<%@  MasterPageFile="~/Site.Master" %>--%>
<%--<%@ MasterType TypeName="Delivery.SiteMaster" %>--%>
<%--<%@ MasterType VirtualPath="~/Site.Master" %>--%>
<%@ MasterType TypeName="Delivery.SiteMaster" %>

My Designer Control (MenuEsquerdo.ascx.designer.cs)

public new Delivery.SiteMaster Master {
            get {
                return ((Delivery.SiteMaster)(base.Master)); // 'System.Web.UI.UserControl' does not contain a definition for 'Master'
            }
        }

And my code behind (MenuEsquerdo.ascx.cs)

RepeaterListaMenuEsquerdoCtr.DataSource = this.Master.ClassMercadorias.Take(20);
RepeaterListaMenuEsquerdoCtr.DataBind();

Know, how I can fix this: 'System.Web.UI.UserControl' does not contain a definition for 'Master'?

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