Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

add to menu item in code behind

Posted on 2009-05-19
5
Medium Priority
?
1,285 Views
Last Modified: 2013-12-17
I'm trying to figure out how to add a new item to the asp.net menu control. Below is what I've got. There is a parent level: Reqs and all users can add, view open and view closed. However, I only want an admin to have the ability to edit a req.

How do I achieve this from the code behind?
<asp:MenuItem Text="Reqs" Value="Reqs">
            <asp:MenuItem NavigateUrl="~/secure/addreq.aspx" Text="Add a Req"
                Value="Add a Req"></asp:MenuItem>
            <asp:MenuItem NavigateUrl="~/secure/openreqs.aspx" Text="Open Reqs" 
                Value="Open Reqs"></asp:MenuItem>
            <asp:MenuItem NavigateUrl="~/secure/closedreqs.aspx" Text="Closed Reqs" 
                Value="Closed Reqs"></asp:MenuItem>
        </asp:MenuItem>
 
admins - only should have this view:
<asp:MenuItem Text="Reqs" Value="Reqs">
            <asp:MenuItem NavigateUrl="~/secure/addreq.aspx" Text="Add a Req"
                Value="Add a Req"></asp:MenuItem>
            <asp:MenuItem NavigateUrl="~/secure/openreqs.aspx" Text="Open Reqs" 
                Value="Open Reqs"></asp:MenuItem>
            <asp:MenuItem NavigateUrl="~/secure/closedreqs.aspx" Text="Closed Reqs" 
                Value="Closed Reqs"></asp:MenuItem>
            <asp:MenuItem NavigateUrl="~/secure/updatereqs.aspx" Text="Update Reqs" 
                Value="Update Reqs"></asp:MenuItem>
        </asp:MenuItem>
 
nav.ascx.cs
protected void Page_Load(object sender, EventArgs e)
    {
        System.Web.Profile.ProfileBase p = HttpContext.Current.Profile;
        MembershipUser currentMember = Membership.GetUser(p.UserName.ToString());
 
        if (Request.IsAuthenticated)
        {
            if (Roles.IsUserInRole(currentMember.UserName, "PortalAdmin"))
            {
                //add the following - <asp:MenuItem NavigateUrl="~/secure/updatereqs.aspx" Text="Update Reqs" 
                //Value="Update Reqs"></asp:MenuItem>
 
            }
        }
        else
        {
            Response.Redirect("../login.aspx");
        }
    }

Open in new window

0
Comment
Question by:fwstealer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 7

Expert Comment

by:NicoJuicy
ID: 24423803
Make a sitemap xml.

The first sitemap xml show all the links that can be viewed by users.
The second sitemap xml shows the links that can be viewed by the admin.

In your masterpage create a function to check if a admin has logged in, if it's an admin who logged in you set the second sitemap xml to visible.

I have already used this in a previous project (where their are logged in users and users who aren't logged in)

This is praticly the same.

(ps. don't forget to bind your menu to an xml datasource)

Hope this helps.
0
 

Author Comment

by:fwstealer
ID: 24424667
never done that before, so are two xml files or just one with separate nodes?
0
 
LVL 7

Expert Comment

by:NicoJuicy
ID: 24425543
2 different xml files with 2 different datasources.

I'll show you some code i used once.

CODE BEHIND Master.master (vb)
 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'nog aan te passen
 
        Select Case CultureInfo.CurrentCulture.Name.ToString.Substring(0, 2)
            Case "nl"
                'Selecteer nederlandstalige xml advertisement
                AdRotator1.AdvertisementFile = "AdRotatorNl.xml"
            Case "fr"
                'Franstalige advertisement
                AdRotator1.AdvertisementFile = "AdRotatorFR.xml"
            Case Else
                'engelstalige advertisement
                AdRotator1.AdvertisementFile = "Adrotator.xml"
        End Select
        login(Session("login"))
    End Sub
 
    Public Sub login(ByVal login As Boolean)
        If login = True Then
            loggedIn()
        Else
            notLoggedIn()
        End If
    End Sub
    Private Sub loggedIn()
        menuLoggedIn.Visible = True
        menuLoggedOut.Visible = False
    End Sub
    Private Sub notLoggedIn()
        menuLoggedIn.Visible = False
        menuLoggedOut.Visible = True
    End Sub
 
Asp.Net code
 
Datasource of the menu's
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/loggedIn.xml">
</asp:XmlDataSource>     
 
<asp:XmlDataSource ID="XmlDataSource2" runat="server" 
DataFile="~/loggedout.xml"></asp:XmlDataSource>
 
And my menu's (with css included)
<asp:Menu ID="menuLoggedIn" runat="server" DataSourceID="XmlDataSource1" 
            StaticDisplayLevels="2" Width="208px" MaximumDynamicDisplayLevels="2">
               <LevelSubMenuStyles>
                <asp:SubMenuStyle BackColor="#EAF9C2" BorderColor="White" BorderStyle="Solid" 
                    BorderWidth="1px" Font-Underline="False" Height="40px" VerticalPadding="12px" />
            </LevelSubMenuStyles>
            <LevelMenuItemStyles>
                <asp:MenuItemStyle BackColor="#81AC00" Font-Names="Lucida Sans" Font-Size="Small" 
                    Font-Underline="False" ForeColor="White" Height="40px" 
                    HorizontalPadding="12px" BorderStyle="Solid" VerticalPadding="16px" 
                    BorderColor="White" BorderWidth="1px" Font-Bold="True" />
            </LevelMenuItemStyles>
            <StaticMenuItemStyle BackColor="#EAFAC2" BorderColor="#D5E8AD" 
                BorderStyle="Solid" BorderWidth="1px" Height="40px" />
            <DynamicMenuStyle BackColor="#EDFCC3" Height="40px" />
            <DynamicMenuItemStyle BackColor="#81AC00" Font-Names="Lucida Sans" 
                Font-Size="Small" ForeColor="White" Height="40px" Font-Bold="True" />
            <DataBindings>
                <asp:MenuItemBinding DataMember="siteMapNode" 
                NavigateUrlField="url" TextField="title" ToolTipField="description" 
                ValueField="title" />
            </DataBindings>
        </asp:Menu>
        
        
        <asp:Menu ID="menuLoggedOut" runat="server" DataSourceID="XmlDataSource2" 
            StaticDisplayLevels="2" Width="208px">
            <LevelSubMenuStyles>
                <asp:SubMenuStyle BackColor="#EAF9C2" BorderColor="White" BorderStyle="Solid" 
                    BorderWidth="1px" Font-Underline="False" Height="40px" VerticalPadding="12px" />
            </LevelSubMenuStyles>
            <LevelMenuItemStyles>
                <asp:MenuItemStyle BackColor="#81AC00" Font-Names="Lucida Sans" Font-Size="Small" 
                    Font-Underline="False" ForeColor="White" Height="40px" 
                    HorizontalPadding="12px" BorderStyle="Solid" VerticalPadding="16px" 
                    BorderColor="White" BorderWidth="1px" Font-Bold="True" />
            </LevelMenuItemStyles>
            <StaticMenuItemStyle BackColor="#EAFAC2" BorderColor="#D5E8AD" 
                BorderStyle="Solid" BorderWidth="1px" Height="40px" />
            <DynamicMenuStyle BackColor="#EDFCC3" Height="40px" />
            <DynamicMenuItemStyle BackColor="#81AC00" Font-Names="Lucida Sans" 
                Font-Size="Small" ForeColor="White" Height="40px" Font-Bold="True" />
    
            <DataBindings>
                <asp:MenuItemBinding DataMember="siteMapNode" 
                NavigateUrlField="url" TextField="title" ToolTipField="description" 
                ValueField="title" />
            </DataBindings>
        </asp:Menu>

Open in new window

0
 
LVL 7

Accepted Solution

by:
NicoJuicy earned 2000 total points
ID: 24425560
Ow yeah, here's an example of appropriate xml files

'loggedin.xml
 
<?xml version="1.0" encoding="utf-8" ?>
 
<siteMapNode url="" title="Mijn Account"  description="Hier kunt u uw account informatie opvragen en aanpassen">
  <siteMapNode url="Winkelwagen.aspx" title="Winkelwagen"  description="Bekijk uw winkelwagentje" />
  <siteMapNode url="MijnProfiel.aspx" title="Profiel"  description="Bekijk en/of bewerk uw profiel" />
  <siteMapNode url="Historiek.aspx" title="Historiek"  description="Bekijk een lijst van vorige aankopen" />
  <siteMapNode url="Uitloggen.aspx" title="Uitloggen"  description="Log uit met uw account" />
</siteMapNode>
 
'loggedout.xml
<?xml version="1.0" encoding="utf-8" ?>
<siteMapNode url="" title="Account"  description="Account handelingen">
  <siteMapNode url="Inloggen.aspx" title="Inloggen"  description="Hier kunt u inloggen op WaWaWa-Online" />
  <siteMapNode url="Registreren.aspx" title="Registreren"  description="Hier kunt u zich registreren op WaWaWa-Online" />
  <siteMapNode url="WwVergeten.aspx" title="Wachtwoord vergeten"  description="Hier kunt u uw wachtwoord opvragen" />
</siteMapNode>

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question