Master Page content filtering with respect to asp

2019-08-11 05:37发布

I have a Master page having title and menu, now i want if i open login.aspx page, the menu of the master page should be hided, rest title will remain there, means master page is required but without menu, what is the best solution for this?

3条回答
Summer. ? 凉城
2楼-- · 2019-08-11 05:41

Get the menu control and set its Visible property to false Warning: this good is finding control with hard coded ID so it may trow NullReferenceException

Menu menu = Page.Master.FindControl("Menu1");
menu.Visible = false;
查看更多
仙女界的扛把子
3楼-- · 2019-08-11 06:02

You can have a property to show or hide menu in the master page.

like

public void ShowMenu
{
    get { return Menu.Visible; }
    get { Menu.Visible = value; }
}

Note: if the menu is static, you can surround it in PlaceHolder control and manage showing/hiding it using this control. I chose this specific control to suggest as it doesn't render extra HTML so, nothing changes in page.

.

Then in the login page, say Page Load or something (not in pre init or such early times, to have master page created already):

protected void Page_Load(object sender, EventArgs e)
{
    var siteMasterPage =  Page.Master as SiteMasterPageClassName;
    if(siteMasterPage != null) siteMasterPage.ShowMenu = false;
}

.

Update

Another way to solve this is to have nested master-pages. The child master page has the menu and other stuff and is the default master-page for all pages. The parent master-page has all the important stuff that applies even to the login page.

If you already have a master page, you can create another one, move most stuff to the other one from your existing master page, use the same IDs for content place holders, and then make the existing master-page itself have an masterpage file set to the new one, and then it should be easy to go to login page and also change the master-page file name to the new master-page file.

查看更多
霸刀☆藐视天下
4楼-- · 2019-08-11 06:02

You can do like the following. Put this in master page's code behind.

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Url.AbsoluteUri.Contains("Login.aspx"))
    {
        //Disable Menu here
    }
}
查看更多
登录 后发表回答