2011-03-30 63 views
2

我有一個簡單的母版頁(mas​​ter.aspx),它有3個鏈接按鈕,即HomeLBTn--將用戶重定向到home.aspx,LoginLBtn-到login.aspx,RegisterLBtn-到Register.aspx關於母版頁的問題

我的項目中的每一頁都繼承了母版頁。 home.aspx也繼承master.aspx,但我不希望home.aspx繼承HomeLBtn,我希望它繼承剩餘的2個LBtn,但不是HomeLBtn。我怎樣才能將這一情況到Home.aspx

請幫我在期待

感謝

回答

4

一個辦法是找到在母版的控制,並設置其可見性設置爲false:

Page.Master.FindControl("HomeLBtn").Visible = False 

這將在Page_Load(或其他生命週期事件)頁面上進行不應該顯示主頁按鈕。

0

你可以在母版頁是這樣的(檢查子頁面是什麼)

//If the child page is viewing an order then disable the new request button  
if (this.ContentPlaceHolder1.Page is OrderView)  
{ 
     base.RemoveMenuItem("New Request", mnuSettings);   
} 

注:base.RemoveMenuItem是我的基地頁

http://wraithnath.blogspot.com/2010/09/how-to-hide-show-controls-on-master.html

的方法

另一種選擇是將屬性添加到子頁面和母版頁並使用會話數據隱藏和顯示按鈕。

例如。在子頁面:

protected MyCustomMasterPage CustomMasterPage 
{ 
    get 
    { 
    return this.Master as MyCustomMasterPage; 
    } 
} 

在你的主頁,你可以有你可以設置爲隱藏會話變量,並顯示按鈕

public bool HomeVisible 
{ 
    get 
    { 
     return (bool) Session["HomeVisible"]; 
    } 
    set 
    { 
     Session["HomeVisible"] = value; 
    } 
} 

你會再加載母版頁時檢查HomeVisible財產顯示/隱藏按鈕。然後你可以從子頁面設置它。

this.MyCustomMasterPage.HomeVisible = false; 

也許不是最好的方式,但他們的工作

+0

這是不可擴展的。如果他有更多的頁面需要這個呢?最好在具有特定需求的每個頁面上執行此操作。您在母版頁上執行此操作時會增加開銷,因爲沒有特殊需求的頁面仍然需要運行此邏輯 – 2011-03-30 16:29:51

+0

我同意,另一個選項是將屬性添加到子頁面以投射母版頁並在其中檢查。我把它添加到解決方案。 – WraithNath 2011-03-30 16:31:43

0

在master.aspx,確定註冊並登入鏈接按鈕,並在那裏Home鍵會去一個內容佔位符。然後讓Home.aspx繼承master.aspx。然後創建從master.aspx繼承的第二個母版頁(mas​​ter2.aspx)。在master2中,在內容佔位符中添加Home鏈接按鈕,並讓其他頁面從master2繼承。