ASP.NET - 从内容页访问母版页元素

发布于 2024-07-13 06:49:02 字数 199 浏览 5 评论 0原文

可以从内容页访问母版页的元素吗?

假设我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风和你 2024-07-20 06:49:02

是的...如果您需要使用 MasterPage 从 aspx 页面执行此操作,则为:

Button myButton = (Button)Master.FindControl("myButton");
myButton.Visible = false;

Yes...if you need to do this from the aspx page using the MasterPage it would be:

Button myButton = (Button)Master.FindControl("myButton");
myButton.Visible = false;
溺深海 2024-07-20 06:49:02

您必须在页面/用户控件标记中放置对 MasterPage 的引用。

<%@ Reference VirtualPath="..." %>

然后在代码隐藏中,您只需将 Page.MasterPage 转换为 MasterPage 并访问其属性即可。

MyMasterPage myMasterPage = (MyMasterPage)Page.Master;

You have to put a reference to the MasterPage in your page/user control markup.

<%@ Reference VirtualPath="..." %>

Then in the code-behind, you just cast the Page.MasterPage to your MasterPage and access its properties.

MyMasterPage myMasterPage = (MyMasterPage)Page.Master;
少女情怀诗 2024-07-20 06:49:02

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.

撩起发的微风 2024-07-20 06:49:02

是的,他们可以,并且有几种方法可以实现这一点。

我使用的方法是在母版页中创建公共方法,该方法将对母版页中的数据进行修改/访问。 例如,我通常喜欢修改我所在的当前页面/类别的链接样式,因此我的母版页中有一个如下所示的方法:

   Public Sub SetNavigationPage(ByVal MenuName As String)

      DirectCast(Me.FindControl(MenuName), HyperLink).CssClass = "MenuCurrent"

   End Sub

然后在我的内容页面中,我只需按如下方式访问此方法:

Dim myMaster As EAF = DirectCast(Me.Master, EAF)
myMaster.SetNavigationPage("hypViewEmployee")

...其中 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:

   Public Sub SetNavigationPage(ByVal MenuName As String)

      DirectCast(Me.FindControl(MenuName), HyperLink).CssClass = "MenuCurrent"

   End Sub

Then in my content page, I simply access this method as such:

Dim myMaster As EAF = DirectCast(Me.Master, EAF)
myMaster.SetNavigationPage("hypViewEmployee")

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

迷荒 2024-07-20 06:49:02

我遇到了一个问题,除非在 ContentPlaceHolder 中查找,否则无法确定控件。 在本例中为标签 (Label.ID = "lblMstrMessage")。
我的代码如下(其中ContentPlaceHolder.ID =“ContentTopPortion”)

  Control cph = this.Master.FindControl("ContentTopPortion");
  Label mpLabel = (Label)cph.FindControl("lblMstrMessage");

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

  Control cph = this.Master.FindControl("ContentTopPortion");
  Label mpLabel = (Label)cph.FindControl("lblMstrMessage");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文