C# 如何从子页面控制asp.net母版页上的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1092784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to control elements on a asp.net master page from child page
提问by Lloyd
I have a few pages on my asp.net website that I would like to turn off a control on the master page. Is there a way to communicate with the master page from a child page?
我的 asp.net 网站上有几个页面,我想关闭母版页上的控件。有没有办法从子页面与母版页进行通信?
采纳答案by Ahmad Mageed
Easiest way is to setup a property on your master page that handles the on/off functionality when called. Then in your child page set the MasterType directiveto get a strongly typed reference to your master page to bypass the need to cast.
最简单的方法是在母版页上设置一个属性,在调用时处理开/关功能。然后在您的子页面中设置MasterType 指令以获取对您的母版页的强类型引用以绕过强制转换的需要。
Your child page would have:
您的子页面将具有:
<%@ MasterType VirtualPath="~/Site1.Master" %>
And to call the property of the master page:
并调用母版页的属性:
Master.MyLabel = false; // or true
So on your master you could have:
所以在你的主人上你可以有:
public bool MyLabel
{
get
{
return masterLabel.Enabled;
}
set
{
masterLabel.Enabled = value;
}
}
回答by Joe
Here's an example of how to communicate with Master Pages from Child Pages.
下面是如何从子页面与母版页面通信的示例。
In the master page, create a property for the control you wish to access, say a label called lblUser...
在母版页中,为要访问的控件创建一个属性,例如一个名为 lblUser 的标签...
public string MyProperty
{
set { lblUser.Text = value; }
}
In the child page, access the property you have just created as in the following example...
在子页面中,访问您刚刚创建的属性,如下例所示...
((MyMaster)this.Master).MyProperty = "Text changed from Sub Page";
We are able to do this because the Master is simply a class that inherits System.Web.UI.MasterPage.
我们能够这样做是因为 Master 只是一个继承 System.Web.UI.MasterPage 的类。
And the Label.Text property that we created is just a property of the Master class (this name will be the specific name of your current Master Page, in this case MyMaster).
我们创建的 Label.Text 属性只是 Master 类的一个属性(此名称将是您当前母版页的特定名称,在本例中为 MyMaster)。
Notice it won't work without a cast to your specific Master Page class and this is because you added a field that does not exist in System.Web.UI.MasterPage, the type of this.Master, but in your specific Master Class.
请注意,如果不强制转换到您的特定母版页类,它将无法工作,这是因为您添加了一个在 System.Web.UI.MasterPage 中不存在的字段,即 this.Master 的类型,但在您的特定母版类中。
回答by Colin
this.Master.FindControl("controlid").dosomethinghere
回答by Mark Dickinson
You need to use a MasterType directive in your page markup
您需要在页面标记中使用 MasterType 指令
<%@ MasterType TypeName="Namespace.Etc.MyMasterPage" %>
Then you will be able to access any public properties of the page's master page using
然后,您将能够使用访问页面母版页的任何公共属性
this.Master.PropertyIWantToIntefereWith...
Hope this helps
希望这可以帮助
回答by Ahmad Mageed
I was looking for something similar, but this solution doesn't seem to work for me. I have a nested master page. Does this work for a nested master? When I implemented, it's not recognizing the control that I added to my master.
我正在寻找类似的东西,但这个解决方案似乎对我不起作用。我有一个嵌套的母版页。这对嵌套母版有用吗?当我实施时,它没有识别我添加到我的主人的控件。