2012-04-14 20 views
1

我有一個導航到登錄頁面的菜單項「登錄」。當用戶登錄時,我將該菜單項的文本更改爲「註銷」。點擊後,我希望它回去清除會話,轉到登錄頁面,文本更改回「登錄」。這工作...有點。我遇到的問題是我不知道如何在點擊菜單項時調用函數,因此當用戶單擊「註銷」時,加載登錄頁面並在頁面加載時清除會話,但由於它是在頁面加載之前沒有清除,它仍然顯示有會話(用戶名仍然出現,並且菜單項文本未被更改)。如何設置它,以便會話在「註銷」中被清除而不是在登錄頁面加載時被清除?設置MenuItem點擊事件來呼叫註銷功能

這裏是我的菜單代碼:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="LeftSideMenu.ascx.cs" Inherits="EVMAnywhereWeb.Controls.LeftSideMenu" %> 

<div id="leftSideMenu"> 
<asp:menu ID="menu1" runat="server" Orientation="Vertical" RenderingMode="List"> 
<StaticMenuItemStyle VerticalPadding="5" /> 
<DynamicMenuItemStyle VerticalPadding="5" /> 
<Items> 
    <asp:menuitem navigateurl="~/Login.aspx" Text="Login" Value="Login"></asp:menuitem> 
    <asp:menuitem navigateurl="~/Register.aspx" Text="Register" Value="Register"></asp:menuitem> 
    <asp:menuitem navigateurl="~/Projects.aspx" Text="Projects" Value="Projects"></asp:menuitem> 
    <asp:menuitem navigateurl="~/Dictionary.aspx" Text="Dictionary" Value="Dictionary"></asp:menuitem> 
</Items> 
</asp:menu> 
</div> 

這裏是我用來更改文本(代碼隱藏)代碼:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session["SessionUserName"] != null) 
     { 
      menu1.Items[0].Text = "Logout"; 
      menu1.Items[1].Text = "My Account"; 

     } 
    } 

而在登錄頁面代碼隱藏,我請清除會話:

protected void Page_Load(object sender, EventArgs e) 
    { 

     if (Session["SessionUserName"] != null) 
     { 
      Session.Abandon(); 
      Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1)); 
      Response.Cache.SetCacheability(HttpCacheability.NoCache); 
      Response.Cache.SetNoStore(); 

     } 
    } 

回答

1

調用您的代碼來清除OnInit中的會話而不是Page_Load。

+0

除非我這樣做是錯誤的(這可能是)OnInit不適合我。如果(Session [「SessionUserName」]!= null) { Session.Abandon();}} public new void OnInit(EventArgs e) { if Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1)); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetNoStore(); } }' 我試着使用重寫而不是新的,但那是因爲我沒有任何重寫。 OnInit根本不會被調用。 – 2012-04-14 22:19:22

+0

OnInit始終被調用。我認爲你需要保護方法。例如: 保護覆蓋無效OnInit(EventArgs e){...} – 2012-04-14 22:33:16

+0

好的。所以那個工作......主要是。雖然我有同樣的問題,但我可能無法改變這一點。會話正在清除,但菜單項的文本不會更改回「登錄」。我可能需要告訴它改回來,但隨着文本在菜單控制的代碼中被更改,我不認爲我能夠改變它。現在,在我註銷並且會話被清除後,如果再次點擊任何菜單項,文本就會改變。只是當用戶實際註銷時。 – 2012-04-14 22:39:51