ASP.NET - 从内容页访问母版页元素
可以从内容页访问母版页的元素吗?
假设我有 MasterPage1 和从 MasterPage1 继承的 ContentPage1,并且 MasterPage1 有一个按钮:Button1。
我可以从内容页面更改该按钮的属性,例如使 Button1 不可见、不活动等吗? 我怎样才能做到这一点?
我使用的是.net2.0
Can the elements of the Master Page be accessed from the Content Page?
Lets say I have MasterPage1 and ContentPage1 that inherits from the MasterPage1, and the MasterPage1 has a button: Button1.
Can I change the property of that button from the content page, for example to make Button1 invisible, inactive etc? How can I accomplish this?
I am using .net2.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的...如果您需要使用 MasterPage 从 aspx 页面执行此操作,则为:
Yes...if you need to do this from the aspx page using the MasterPage it would be:
您必须在页面/用户控件标记中放置对 MasterPage 的引用。
然后在代码隐藏中,您只需将 Page.MasterPage 转换为 MasterPage 并访问其属性即可。
You have to put a reference to the MasterPage in your page/user control markup.
Then in the code-behind, you just cast the Page.MasterPage to your MasterPage and access its properties.
Master.FindControl("myButton").Visible = False
请注意,用于运行上述命令的控件不应位于“更新”面板内。
Master.FindControl("myButton").Visible = False
Be careful that the control that you use to run the above command, should not be inside an Update panel.
是的,他们可以,并且有几种方法可以实现这一点。
我使用的方法是在母版页中创建公共方法,该方法将对母版页中的数据进行修改/访问。 例如,我通常喜欢修改我所在的当前页面/类别的链接样式,因此我的母版页中有一个如下所示的方法:
然后在我的内容页面中,我只需按如下方式访问此方法:
...其中 EAF 是我的母版页的类名称。
我发现的一个有趣的问题是,当我尝试以这种方式显示/隐藏 .NET 控件的 Visibility 属性时,我遇到了一些复杂的情况。 这是由于母版页和内容页的渲染顺序造成的。 为了解决这个问题,我为可见和隐藏设置了基本的 CSS 样式,并相应地设置了 CssClass 属性。
Yes they can, and there are a few approaches to this.
The approach I use is to create public methods within the master page that will do the modification/access to the data within the master page. For example, I typically like to modify the link style of the current page/category I am on, so I have a method in my master page like this:
Then in my content page, I simply access this method as such:
...where EAF is the name of the class of my master page.
One interesting issue I've found is that I've had complications with using the Visibility property of .NET controls when trying to show/hide them in this manner. This is due to the rendering orer of master and content pages. To resolve this, I setup a basic CSS style for both visible and hidden and set the CssClass property accordingly.
我遇到了一个问题,除非在 ContentPlaceHolder 中查找,否则无法确定控件。 在本例中为标签 (Label.ID = "lblMstrMessage")。
我的代码如下(其中ContentPlaceHolder.ID =“ContentTopPortion”)
I ran into an issue where I couldn't determine the control unless I looked for it within the ContentPlaceHolder. In this case a label (Label.ID = "lblMstrMessage").
My code follows (where ContentPlaceHolder.ID = "ContentTopPortion")