Link to home
Start Free TrialLog in
Avatar of pallosp
pallosp

asked on

Change menu programmatically

I have a web site consisting of
- MasterPage.master containing a menu
- Web.sitemap that provides the menu items (Root (hidden), Home, RSS)
- Default.aspx containing a radio button list
- Rss.aspx

I want to change the query string of the rss feed in the menu depending on the selected item in the radio list

My project is based on http://www.codeproject.com/aspnet/ABetterSiteMapResolve.asp
I created the PageCode.cs in the Support project, added a reference to it, and changed the parent class of Default.aspx.cs.
What should be written into the overridden OnSiteMapResolve event handler? I've been experimenting a lot, but I haven't been managed to change anything in the menu.

   protected override SiteMapNode OnSiteMapResolve(SiteMapResolveEventArgs e) {
      //...
   }

The event handler is surely called, because if a throw new Exception() line is inserted before the return, the exception is thrown. But neither the caption, nor the url of the menu items change. What is the solution?
ASKER CERTIFIED SOLUTION
Avatar of deanvanrooyen
deanvanrooyen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pallosp
pallosp

ASKER

The menu is on the master page, so I can't access it directly.
Something is still wrong, because ((Menu)Master.FindControl("MainMenu")).Items.Count is 0.


 Menu m = (Menu)this.Master.FindControl("MainMenu");

        m.Items[0].ChildItems[0].Text = "test";
        m.Items[0].ChildItems[0].NavigateUrl = "test.html";
Avatar of pallosp

ASKER

I think m.Items contains the items created by the designer, not the items from the site map data source.
Probably this is the reason of that m.Items[0] doesn't exist.
Avatar of pallosp

ASKER

I get the following error message:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Line 101:        m.Items[0].ChildItems[0].Text = "test";
is m null? did it actually reference the menu?
Avatar of pallosp

ASKER

m is not null, m.DataSourceID is "MainMenuDS" as it should be, but m.Items is a zero long collection.
m.DataSource is null.
did you get any where?

how is the menu bound?
Avatar of pallosp

ASKER

masterpage.master:

<asp:Menu ID="MainMenu" runat="server" DataSourceID="MainMenuDS" Orientation="Horizontal" >
     <StaticMenuItemStyle HorizontalPadding="2px" />
</asp:Menu>
<asp:SiteMapDataSource ID="MainMenuDS" runat="server" ShowStartingNode="False"/>

web.sitemap:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap enableLocalization="true" xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
  <siteMapNode url="" title="Main"  description="" roles="*">
    <siteMapNode url="Default.aspx" title="Kezd&#337;lap" resourceKey="HomePage" />
    <siteMapNode url="rss.aspx" title="RSS" resourceKey="RSS" />
  </siteMapNode>
</siteMap>

The menu is bound to a SiteMapDataSource that reads the web.sitemap.
When Default.aspx is selected from the menu, I want to modify the url of the RSS link depending on the selected item of the radio button list.

Unfortunately I couldn't reach this behavior in the OnSiteMapResolve event no way.
Avatar of pallosp

ASKER

TheLearnedOne: what is the condition of refunding points?
I'm not affected because of the unlimited points, just being curious.
its a pity

the points are not the issue, why doesnt it work? I would like to know... can you load the project somewhere with just the basics of the page and code that is not working. there could be something else binding on the page that is causing some issues here.
Avatar of pallosp

ASKER

I tried to reproduce the issue in a new project, and found the difference:
Because of the Intellisense I accidentally overrode the OnPreRender method instead of OnPreRenderComplete. In OnPreRender the menu isn't already filled by the sitemap.

Microsoft's solution at http://msdn2.microsoft.com/en-us/library/ms178425.aspx is still dead, the menu remains unchanged.

Thanks
Hi Mate

Just checking on how diod you with this solution as i have a similar situtation where i got a home page that has two link that pass a querystring SiteID. when you click on that link it calls a master page that loads a menu depending on the querystring SiteID and on menu click it call the contents etc.
the problem that that when i select the link on the home page teh menu of the masterpage doesnt change, it shows me the same menu for all the siteID. though i have checked that the querystring is passed correctly etc.