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

时间:2021-03-23 21:09:49

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

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

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?)

'System.Web.UI.MasterPage'不包含'DisplayMessage'的定义,并且没有可以找到接受类型'System.Web.UI.MasterPage'的第一个参数的扩展方法'DisplayMessage'(你是否缺少using指令?或汇编参考?)

Any help will be appreciated.

任何帮助将不胜感激。

3 个解决方案

#1


6  

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指令添加到页面标记中(在顶部,标准<%@ Page%>指令所在的位置):

<%@ 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.

MasterType指令可从.NET 2向上提供。

#2


5  

You need to cast the Master to the actual Masterpage type

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

// 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指令以实现相同的目的:

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

Then you can use:

然后你可以使用:

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

#3


0  

I made/try this:

我做了/尝试这个:

My Control (MenuEsquerdo.ascx)

我的控件(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)

我的设计师控件(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)

我的代码背后(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'?

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

#1


6  

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指令添加到页面标记中(在顶部,标准<%@ Page%>指令所在的位置):

<%@ 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.

MasterType指令可从.NET 2向上提供。

#2


5  

You need to cast the Master to the actual Masterpage type

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

// 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指令以实现相同的目的:

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

Then you can use:

然后你可以使用:

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

#3


0  

I made/try this:

我做了/尝试这个:

My Control (MenuEsquerdo.ascx)

我的控件(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)

我的设计师控件(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)

我的代码背后(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'?

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

相关文章