问题
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?
回答1:
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;
回答2:
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.
回答3:
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
}
}
来源:https://stackoverflow.com/questions/5893239/master-page-content-filtering-with-respect-to-asp-page