开发者

Different menu in master.page depending on login type

开发者 https://www.devze.com 2023-03-17 07:23 出处:网络
I have 2 types of users (phonebookers and salesrep). They are supposed to have 2 different menu bars, pending on their login.

I have 2 types of users (phonebookers and salesrep). They are supposed to have 2 different menu bars, pending on their login.

From the login.aspx - I set a number of Session[]s among them an ["EmployeeType"]. In my master page I have a ContentPlaceholder ID="Menu".

Master.page seems locked in place, long before I can change anything pending some Session value. I found the load sequence which seems to confirm my suspession as Masterpage is loaded and locked way before any contentpage get in play.

I tried <% If (Session["EmployeeType"].ToString() == "1") { %> type 1 employee <% } else {%>Type 2 employee<% } %> - without luck.

I tried doing it in Page_PreRender(EventArgs e) - no luck.

I tried having the hyperlink controls in the masterpage, without a text and then setting the text later i PreRender and visible = true/false - no luck.

Google says开发者_开发问答 there is an option with javascript but I haven't found any examples or I haven't recognised any javascript as something I could use.

Google found a fair few people having the opposite problem of mine, their masterpage loads way too many times and they want to stop it doing that.


I would avoid doing logic in your masterpage, it tends to get a bit messy.

What I would do is to create two panels and hide one of them by default, the one that is least common to be shown. And then Enable the appropriet panel from your masterpage code behind like this:

<div id="menu">
    <asp:Panel ID="UserMenu" runat="server">
        <MenuControl:UserMenu />
    </asp:Panel>
    <asp:Panel ID="AdminMenu" runat="server" Visible="false">
        <MenuControl:AdminMenu />
    </asp:Panel>
</div>

The Page_Load method looks like this now:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["EmployeeType"].ToString() == "1")
    {
        AdminMenu.Visible = true;
        UserMenu.Visible = false;
    }
    else
    {
        AdminMenu.Visible = false;
        UserMenu.Visible = true;
    }
}

This will show my User Control AdminMenu if it is an Employee with EmployeeType equal to 1 and otherwise show the UserMenu User Control.


Try:

  <% If (Session["EmployeeType"].ToString()  == "1") { %> type 1 employee <% } else {%>Type 2 employee<% } %>


I recommend defining some public static methods in the master page class to set different types of menu:

public partial class SiteMaster : System.Web.UI.MasterPage
{
    // ...

    public static void SetEmployeeMenu(MasterPage master)
    {
        // do whatever you want on master page
        ((SiteMaster)master).EmployeeMenu.Style.Add("display", "");
    }
}

And then on your content page load event decide which menu you want to display and use these functions:

SiteMaster.SetEmployeeMenu(this.Master);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号