ASP.Net - How to access the main page of an object from a regular page?

In the project I'm working on, the codebehind main page loads complex checks and confirmations that determine the navigation list displayed in the TreeView on the page. Now I need a way to access this list from another front-end page, for example, "frontpage.aspx".

This serves two purposes. Firstly, the main page will hide pages in the navigation list, which the user should not have access to, but the user can still enter the page by manually entering the page name in the URL. Having the ability to view the TreeView, I can isolate the entire authorization in one method, simply by checking if the page name exists in the currently used TreeView.

Two, this will allow me to easily change the displayed content of any page without checking the database or saving sessions for any specific rights that the current user has, as I can just see if TreeView contains “Product Admin”, for example, and then use this is to hide or show the section of the page that is associated with the "Product Administrator" functionality.

So, any tips on how to do this, or if possible?

+3
source share
3 answers

Assuming frontpage.aspx is a content page, you can definitely access the main page.

, 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

+2

Master, :

TreeView tv = Master.MyTreeViewControl;

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

MSDN .

0

, PageMaster, ;

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

Source: https://habr.com/ru/post/1755489/


All Articles