Question

I'm working on a project that uses a master page and content pages. My masterpage a navigation bar:

<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
        <Items>
            <asp:MenuItem NavigateUrl="~/ProjectPage.aspx" Text="Home" />
            <asp:MenuItem NavigateUrl="~/ProductBacklog.aspx" Text="Product Backlog"/>
            <asp:MenuItem NavigateUrl="~/SprintBacklog.aspx" Text="Sprint Backlog" />
            <asp:MenuItem NavigateUrl="~/MeetingPage.aspx" Text="Meetings" />
            <asp:MenuItem NavigateUrl="~/Burndown.aspx" Text="Burndown"/>
            <asp:MenuItem NavigateUrl="~/About.aspx" Text="About Us"/>
        </Items>
    </asp:Menu>

On one of my content pages, I dynamically add sub-menu menuitems to my 'Sprint Backlog' menuitem. There is a button, and everytime the user clicks that button, a sub-menuitem is added, so that when the user hovers over 'Sprint Backlog' in the navigation menu, the submenu comes up. I do this by creating a list of menuitems, creating a new menuitem with (shown text, value, navigationURL), adding the menuitem to the list of menuitems, then saving the list to Session:

    protected void btSave_Click(object sender, EventArgs e)
    {            
        menuItemList = (List<MenuItem>)Session["menuItemList"];
            if (menuItemList == null)
            {
                menuItemList = new List<MenuItem>();    
            }

            MenuItem menuItem = new MenuItem("Sprint " + sprintNumber, sprintNumber.ToString(), "SprintBacklog.aspx");
            menuItemList.Add(menuItem);
            Session["menuItemList"] = menuItemList;     
    }

In the code-behind for my masterpage, I create a list of menuitems, set the value of the instance of the menuitem from Session, and add childitems to the navigationmenu at the appropriate index. The childitem I am adding are the menuitems from the list of menuitems.

List<MenuItem> menuItemList;

    protected void Page_Load(object sender, EventArgs e)
    {
        menuItemList = (List<MenuItem>)Session["menuItemList"];

        if (menuItemList != null)
        {
            foreach (MenuItem menuitem in menuItemList)
            {
                NavigationMenu.Items[2].ChildItems.Add(menuitem);
            }
        }
    }

I know that I gave these childitems a value when I created them, but my problem is accessing those values when I am loading the SprintBacklog.aspx content page. Whenever a user clicks on one of the childitems, it will always navigate to SprintBacklog.aspx, but the contents of that page should differ according to which child item they clicked. I need a way to know which childitem they clicked, and access that value to populate my content page.

If someone has a better way for me to carry this whole thing out, I am open for suggestions and change. Otherwise, if my setup can work, and there is a way for me to extract the value of the clicked childitem, I'd really like to know that.

I know if I hard-code the childitems in my masterpage, I can easily get the value, but my problem is that I'm creating the submenu childitems dynamically, and I'm not sure how to access it.

Any help would be really appreciated! Thanks!

-Jose

Was it helpful?

Solution

It's been a long time since I asked this question, and I'm not familiar with how masterpages work anymore, but if anyone is experiencing anything similar, I may have a suggestion.

Each menu item I was creating was linking to SprintBacklog.aspx like so:

MenuItem menuItem = new MenuItem("Sprint " + sprintNumber, sprintNumber.ToString(), "SprintBacklog.aspx");

What I should have done was link to SprintBacklog.aspx, but also add a parameter to the request with the sprint ID.

Then the controller which handles the rendering of SprintBacklog.aspx would read the parameter and fetch the appropriate data to render.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top