ASP.Net - 如何从常规页面访问母版页对象?

发布于 2024-09-10 18:13:44 字数 396 浏览 3 评论 0原文

在我正在开发的一个项目中,母版页代码隐藏会执行大量复杂的检查和确认,以决定页面 TreeView 上显示的导航列表。现在,我需要一种从另一个前端页面(例如“frontpage.aspx”)访问此列表的方法。

这有两个目的。第一,母版页会隐藏导航列表中用户不应访问的页面,但用户仍然可以通过手动在 URL 中键入页面名称来进入该页面。通过浏览 TreeView,我可以通过简单地检查当前使用的 TreeView 中是否存在页面名称来将整个授权隔离到单个方法中。

第二,这将允许我轻松更改任何页面的显示内容,而无需检查数据库或存储当前用户拥有的任何特定权限的会话,因为我可以仅查看 TreeView 是否包含“产品管理”,然后使用用于隐藏或显示与“产品管理”功能有关的页面部分。

那么,关于如何做到这一点,或者是否可能,有什么建议吗?

In a project I'm working on, the master page codebehind does a load of complex checks and confirmations that decide the navigation list displayed on the TreeView of the page. Now, I need a way to access this list from another front-end page, such as "frontpage.aspx".

This serves two purposes. One, the masterpage will hide pages on the navigation list the user shouldn't have access to, but the user can still enter the page by typing the page name into the URL manually. By being able to browse through the TreeView, I can isolate the whole authorization into a single method by simply checking if the page name exists within the currently used TreeView.

Two, this will allow me to easily change the displayed content of any page without checking the database or storing sessions for whatever specific rights the current user has, as I can just look if the TreeView contains "Products Admin" for example, and then use that to hide or display the section of the page that has to do with "Product Admin" functionality.

So, any tips on how to do this, or if it's even possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

烏雲後面有陽光 2024-09-17 18:13:44

假设 frontpage.aspx 是一个内容页,您绝对可以从中访问母版页。

例如,此代码将查找母版页上的 TextBox 和 Label 控件。您应该能够调整它以找到您的 TreeView:

// Gets a reference to a TextBox control inside a ContentPlaceHolder
ContentPlaceHolder mpContentPlaceHolder;
TextBox mpTextBox;
mpContentPlaceHolder = 
    (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if(mpContentPlaceHolder != null)
{
    mpTextBox = (TextBox) mpContentPlaceHolder.FindControl("TextBox1");
    if(mpTextBox != null)
    {
        mpTextBox.Text = "TextBox found!";
    }
}

// Gets a reference to a Label control that is not in a 
// ContentPlaceHolder control
Label mpLabel = (Label) Master.FindControl("masterPageLabel");
if(mpLabel != null)
{
    Label1.Text = "Master page label = " + mpLabel.Text;
}

有关详细信息,请参阅 - http ://msdn.microsoft.com/en-us/library/c8y19k6h.aspx

Assuming that frontpage.aspx is a content page you can definitely access the master page from it.

For example this code will find a TextBox and Label controls that are on the master page. You should be able to adapt it to find your TreeView:

// Gets a reference to a TextBox control inside a ContentPlaceHolder
ContentPlaceHolder mpContentPlaceHolder;
TextBox mpTextBox;
mpContentPlaceHolder = 
    (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if(mpContentPlaceHolder != null)
{
    mpTextBox = (TextBox) mpContentPlaceHolder.FindControl("TextBox1");
    if(mpTextBox != null)
    {
        mpTextBox.Text = "TextBox found!";
    }
}

// Gets a reference to a Label control that is not in a 
// ContentPlaceHolder control
Label mpLabel = (Label) Master.FindControl("masterPageLabel");
if(mpLabel != null)
{
    Label1.Text = "Master page label = " + mpLabel.Text;
}

For more info see - http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx

旧情别恋 2024-09-17 18:13:44

您应该能够通过 Master 属性访问它,即:

TreeView tv = Master.MyTreeViewControl;

TreeView tv = (TreeView)Master.FindControl("MyTreeViewControl");

MSDN 上的此页面提供了有关以编程方式使用母版页的更多信息。

You should be able to access it through the Master property, i.e.:

TreeView tv = Master.MyTreeViewControl;

or

TreeView tv = (TreeView)Master.FindControl("MyTreeViewControl");

This page on MSDN has more information about working with master pages programmatically.

幸福不弃 2024-09-17 18:13:44

您可以从母版页访问任何公共函数,引用 Page.Master 并将此属性转换为您的母版页;

((Styles_Master)Page.Master).IsMyProperty = "new value";

You can access any public functions from the masterpage refering to Page.Master and casting this property to your master-page;

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