开发者

ASP.Net - How to access masterpage Object from regular page?

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 with开发者_如何转开发out 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?


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


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.


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";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜