2012-04-20 51 views
0

我有我的母版頁菜單和標籤,我想這取決於用戶登錄的類型進行更新。刪除菜單中的特定項目並通過asp.net中的內容頁面替換新菜單?

首先我刪除從菜單中工作正常,但它沒有顯示在主可達幾的MenuItems頁。相反,只有有限用戶才能看到所有菜單項的舊菜單。當我調試標籤文本顯示我已經設置,但當頁面加載它不更新。

正在使用以下代碼。

Label lbWelcomeMessage = new Label(); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Master.FindControl("CAMenu").Visible = false; 
    } 

    protected void btnLogin_Click(object sender, EventArgs e) 
    { 
     string userName = txtUsername.Text; 
     string password = txtPassword.Text; 
     Common common = new Common(); 
     DataTable tab = new DataTable(); 
     tab= common.GetUserDetails(userName); 
     string firstName = string.Empty; 
     string userPassword = string.Empty; 
     string RoleID=string.Empty; 

     if (tab.Rows.Count == 1) 
     { 
      firstName = tab.Rows[0][2].ToString(); 
      userPassword = tab.Rows[0][4].ToString(); 
      RoleID = tab.Rows[0][5].ToString(); 
     } 
     if (userPassword == password) 
     { 
      if (RoleID != "1") 
      { 
       Menu CAMenu = new Menu(); 
       CAMenu = (Menu)Master.FindControl("CAMenu"); 
       int count = CAMenu.Items.Count; 

       for (int i = 3; i > 0; i--) 
       { 
        string text = CAMenu.Items[i - 1].Text; 
        CAMenu.Items.RemoveAt(i - 1); 
       } 

       lbWelcomeMessage = (Label)Master.FindControl("lbLoginMessage"); 
       lbWelcomeMessage.Text = "Welcome"+" "+ firstName; 
       ((SiteMaster)Page.Master).MyText = lbWelcomeMessage.Text; 
       Response.Redirect("AdHocSMS.aspx"); 
      } 
      else 
      { 
       lbWelcomeMessage = (Label)Master.FindControl("lbLoginMessage"); 
       lbWelcomeMessage.Text = lbWelcomeMessage.Text+" "+firstName ; 
       Response.Redirect("NewTemplate.aspx"); 
      } 

     } 
    } 

回答

0

我所做的就是這樣的事情

在Site.Mater: -

public void CheckRole() 
    { 
     try 
     { 
      if (System.Web.HttpContext.Current.Session.Count > 0) 
      { 
       string firstName = string.Empty; 
       // string userPassword = string.Empty; 
       string RoleID = string.Empty; 
       Common common = new Common(); 
       DataTable tab = new DataTable(); 
       string userName = (string)Session["UserName"]; 
       User user = new User(userName); 
       tab = user.GetUserDetails(userName); 

       if (tab.Rows.Count == 1) 
       { 
        firstName = tab.Rows[0][1].ToString(); 
        RoleID = tab.Rows[0][3].ToString(); 
       } 

       if (RoleID != "1") 
       { 
        int count = CAMenu.Items.Count; 
        if (count == 5) 
        { 
         for (int menuCount = 3; menuCount > 0; menuCount--) 
         { 
          string text = CAMenu.Items[menuCount - 1].Text; 
          CAMenu.Items.RemoveAt(menuCount - 1); 
         } 
        } 
        lbLoginMessage.Text = "Welcome," + " " + firstName; 
        loginStatus.Visible = true; 
       } 
       else 
       { 
        lbLoginMessage.Text = "Welcome," + " " + firstName; 
        loginStatus.Visible = true; 
       } 

      } 
      else 
      { 
       Session.Abandon(); 
       Response.Redirect("~/Login.aspx", true); 
      } 
     } 
     catch (Exception ex) 
     { 
      new Logger().Log("ShortCom.SiteMaster.CheckRole()", ex.Message); 
      Response.Redirect("~/Error.aspx"); 
     } 
    } 

在Login.Apsx: -

protected void Page_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      Master.FindControl("CAMenu").Visible = false; 
      Master.FindControl("loginStatus").Visible = false; 
     } 
     catch (Exception ex) 
     { 
      new Logger().Log("ShortCom.Login.btnLogin_Click(object sender, EventArgs e)", ex.Message); 
      Response.Redirect("~/Error.aspx"); 
     } 
    } 
    protected void LoadMessageBox(string MessageID) 
    { 
     try 
     { 
      messages = new GUIMessages(); 
      popupExtend = new ModalPopupExtender(); 
      lbMessage = (Label)Master.FindControl("label5"); 
      lbMessage.Text = messages.GetGUIMessage(GUIModule.Login, MessageID); 
      popupExtend = (ModalPopupExtender)Master.FindControl("popupExtender"); 
      popupExtend.Show(); 
     } 
     catch (Exception ex) 
     { 
      new Logger().Log("ShortCom.Login.LoadMessageBox(string MessageID)", ex.Message); 
      Response.Redirect("~/Error.aspx"); 
     } 
    } 

    protected void btnLogin_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      string userName = txtUsername.Text; 
      string password = txtPassword.Text; 

      if (userName == string.Empty && password == string.Empty) 
      { 
       LoadMessageBox("5"); 
       txtUsername.Focus(); 


       return; 
      } 
      if (userName == string.Empty) 
      { 
       LoadMessageBox("1"); 
       txtUsername.Focus(); 
       return; 
      } 
      else if (password == string.Empty) 
      { 
       LoadMessageBox("3"); 

       txtPassword.Focus(); 
       return; 
      } 

      User user = new User(userName); 
      DataTable tab = new DataTable(); 
      tab = user.GetUserDetails(userName); 
      string firstName = string.Empty; 
      string userPassword = string.Empty; 
      string RoleID = string.Empty; 
      string userID = string.Empty; 
      Session["UserName"] = userName; 
      if (tab.Rows.Count == 0) 
      { 
       LoadMessageBox("6"); 
       txtPassword.Text = string.Empty; 
       txtUsername.Text = string.Empty; 
       txtUsername.Focus(); 
       return; 
      } 

      if (tab.Rows.Count == 1) 
      { 
       userID = tab.Rows[0][0].ToString(); 
       firstName = tab.Rows[0][1].ToString(); 
       userPassword = tab.Rows[0][2].ToString(); 
       RoleID = tab.Rows[0][3].ToString(); 
       Session["UserID"] = userID; 
      } 
      //if (firstName != userName) 
      //{ 
      // LoadMessageBox("2"); 
      // txtUsername.Focus(); 
      // return; 
      //} 
      //else 
      { 
       if (userPassword == password) 
       { 
        Response.Redirect("~/Default.aspx"); 
       } 
       else 
       { 
        LoadMessageBox("4"); 
        txtPassword.Focus(); 
        return; 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      new Logger().Log("ShortCom.Login.btnLogin_Click(object sender, EventArgs e)", ex.Message); 
      Response.Redirect("~/Error.aspx"); 
     } 
    } 
1

我假設你已經在你的login.aspx中寫了這段代碼。當單擊登錄按鈕時,它會將您帶到另一個頁面,並再次運行頁面的所有生命週期。並且母版頁內容被重置。

這個問題的解決方案可能是。將這個邏輯在母版頁的代碼一樣

protected override void OnInit(EventArgs e) 
    { 
     base.OnInit(e); 
     if (Session.Count == 0 || Session["Username"] == null) 
      Response.Redirect("~/Login.aspx", true); 
     CheckRole(); 
    } 
    public void CheckRole() 
    { 
     if (System.Web.HttpContext.Current.Session.Count > 0) 
     { 
      tab= common.GetUserDetails(Session["Username"]); 
      if (tab.Rows.Count == 1) 
     { 
       firstName = tab.Rows[0][2].ToString(); 
       userPassword = tab.Rows[0][4].ToString(); 
       RoleID = tab.Rows[0][5].ToString(); 
     } 

      if (RoleID != "1") 
      { 
       Menu CAMenu = new Menu(); 
       int count = CAMenu.Items.Count; 

       for (int i = 3; i > 0; i--) 
        { 
        string text = CAMenu.Items[i - 1].Text; 
        CAMenu.Items.RemoveAt(i - 1); 
        } 

    //your label logic 
      lbWelcomeMessage.Text = "Welcome"+" "+ firstName; 
      ((SiteMaster)Page.Master).MyText = lbWelcomeMessage.Text; 
      Response.Redirect("AdHocSMS.aspx"); 
     } 
     else 
     { 
    //Logic 
      Response.Redirect("NewTemplate.aspx"); 
     } 
    } 
    else 
    { 
     Session.Abandon(); 
     Response.Redirect("~/Login.aspx", true); 
    } 
} 

你必須把用戶ID或用戶名在會話這是一個缺點,但每一頁你不必擔心什麼。

生命週期閱讀這篇文章http://msdn.microsoft.com/en-us/library/ms178472.aspx

讓我知道如果它解決與否。

+0

這是剛剛進入無限循環..做你認爲我需要做出任何改變? – Apoorva 2012-04-22 07:28:39

+0

thnx這個偉大的答案。它不完全爲我工作,但我用你的答案的東西,現在我的代碼運作良好:) Thnx噸... – Apoorva 2012-04-22 13:28:35

+1

我很高興它幫助。如果你可以編輯你的文章,並在你的問題後發佈你的解決方案,這將是非常好的,所以其他開發人員可以從你的一些指點:) – 2012-04-23 08:02:43

相關問題