如何从内容页使用母版页中的方法
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用转换来获取母版页类型,如其他人所示,或者您可以添加
MasterType
指令添加到您的页面标记(位于顶部,标准<%@ Page %> 指令所在的位置):
然后在您的页面中代码,您可以使用示例中的内容:
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):Then in your page code, you can have just what you have in your example:
The
MasterType
directive is available from .NET 2 upwards.您需要将
Master
转换为实际的 Masterpage 类型您还可以添加 MasterType 指令添加到内容页面顶部以实现相同的效果:
然后您可以使用:
You need to cast the
Master
to the actual Masterpage typeYou can also add a MasterType directive to the top of your content page to achieve the same thing:
Then you can use:
我做了/尝试这个:
我的控件(MenuEsquerdo.ascx)
我的设计器控件(MenuEsquerdo.ascx.designer.cs)
和我的代码(MenuEsquerdo.ascx.cs)
知道,我如何解决这个问题:'System. Web.UI.UserControl'不包含'Master'的定义?
I made/try this:
My Control (MenuEsquerdo.ascx)
My Designer Control (MenuEsquerdo.ascx.designer.cs)
And my code behind (MenuEsquerdo.ascx.cs)
Know, how I can fix this: 'System.Web.UI.UserControl' does not contain a definition for 'Master'?