2012-07-11 69 views
1

H已經創建了一個帶有菜單div的母版頁。我將索引頁作爲登錄頁面,因此我在index.aspx中使用登錄控件,該索引在母版頁下注冊。如何在主頁面登錄後獲取菜單?

現在我的問題是我怎麼能隱藏菜單DIV這是在母版頁,直到用戶不登錄,用戶以後做成功登錄菜單的div應該出現在用戶

回答

1

我把這個代碼在我的母版

   <% if (HttpContext.Current.User.Identity.IsAuthenticated) { %> 

        <div>navigation html when is authenticated</div> 

       <% } else { %> 

       <div>navigation html when is NOT authenticated</div> 

       <% } %> 
-1

添加一個@的MasterType指令中內容頁面。在該指令中,VirtualPath屬性設置爲母版頁的位置

然後使用Master.FindControl

void Page_Load() 
{ 
// 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 not in 
// a ContentPlaceHolder 
Label mpLabel = (Label) Master.FindControl("masterPageLabel"); 
if(mpLabel != null) 
{ 
    Label1.Text = "Master page label = " + mpLabel.Text; 
} 

}

http://msdn.microsoft.com/en-us/library/xxwa0ff0.aspx

+0

這將標記爲MVC3,你的回答不適用。 – 2012-07-11 17:42:39

+0

它也被標記爲「just」asp.net,所以它適用。也許你應該貼上海報來宣傳。 :-P – Joe 2012-07-11 19:40:28

1

在你的主頁,你可以做這樣的事情:

if (Request.IsAuthenticated) 
{ 
    <p>Welcome back, @User.Identity.Name!</p> 
} 
else 
{ 
    <!-- Put login form here. --> 
} 
相關問題