Solved

Change menu programmatically

Posted on 2006-06-12
15
641 Views
Last Modified: 2012-08-14
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?
0
Comment
Question by:pallosp
  • 7
  • 5
15 Comments
 
LVL 12

Accepted Solution

by:
deanvanrooyen earned 500 total points
ID: 16891914
sorry I havent looked at the codeproject stuff. I  might be way off here anyway but heres something: is it a binding issue?

the reason whay I asked this is because if you use a drag drop feature of visual studio for the menu and sitemap then you will have some issues with the binding of the menu -  that is if you try and make a change then the chnage is undone by the "automatic" binding of the control to the sitemap, you have to pay attention to the page life cycle and where exactly you make the changes as they must be made after the binding occurs.

try this add this to your aspx page -  this is where I would change stuff in the menu....

   protected override void OnPreRenderComplete(EventArgs e)
    {
        this.Menu1.Items[0].ChildItems[0].Text = "test";
        this.Menu1.Items[0].ChildItems[0].NavigateUrl = "test.html";
        base.OnPreRenderComplete(e);
    }
hope this helps
0
 
LVL 9

Author Comment

by:pallosp
ID: 16892003
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.
0
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 16895846


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

        m.Items[0].ChildItems[0].Text = "test";
        m.Items[0].ChildItems[0].NavigateUrl = "test.html";
0
 
LVL 9

Author Comment

by:pallosp
ID: 16896840
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.
0
 
LVL 9

Author Comment

by:pallosp
ID: 16896872
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";
0
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 16908331
is m null? did it actually reference the menu?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Author Comment

by:pallosp
ID: 16909774
m is not null, m.DataSourceID is "MainMenuDS" as it should be, but m.Items is a zero long collection.
m.DataSource is null.
0
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 17100679
did you get any where?

how is the menu bound?
0
 
LVL 9

Author Comment

by:pallosp
ID: 17101037
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.
0
 
LVL 9

Author Comment

by:pallosp
ID: 17101054
TheLearnedOne: what is the condition of refunding points?
I'm not affected because of the unlimited points, just being curious.
0
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 17103348
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.
0
 
LVL 9

Author Comment

by:pallosp
ID: 17103964
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
0
 
LVL 1

Expert Comment

by:Mayank_Agarwal
ID: 20811293
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now