如何在内容页面的母版页中使用方法 [英] How to use a method in a Master Page from a Content Page
问题描述
我正在用C#编写ASP.NET 4应用程序.我有一个母版页,在其中有以下方法:
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.");
我收到此错误:
'System.Web.UI.MasterPage'不包含'DisplayMessage'的定义,并且找不到扩展方法'DisplayMessage'接受类型为'System.Web.UI.MasterPage'的第一个参数(是否丢失了? using指令还是程序集引用?)
'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.
推荐答案
您可以使用铸造来获取您的母版页类型(如其他人所示),也可以添加
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.");
MasterType
指令可从.NET 2或更高版本开始使用.
The MasterType
directive is available from .NET 2 upwards.
这篇关于如何在内容页面的母版页中使用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!