• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 569
  • Last Modified:

navigateurl in meny contro

In VS2005 ASP.NET 2.0 sqlserver 2000.
I have a menu control of 10 menuitems that is defined in a master page.
each menuitem has a navigateURL to a page of the master.
this is a definition of a menuitem:
<asp:MenuItem NavigateUrl="CandSeminar.aspx" Text="Seminar" Value="Seminar" ></asp:MenuItem>
I would like to add a querystring to the navigateurl that is defined in a Label in the page. (label1.text)
How do I write it?
  • 5
  • 5
1 Solution
You could use some codebehind to accomplish this. You'd also need to set the ID= of the menuItem as follows:

<asp:MenuItem ID="myMenuItem" NavigateUrl="CandSeminar.aspx" Text="Seminar" Value="Seminar"  />

Example code in your codebehind:

using System.Web.UI.WebControls;

. . .

MenuItem myMenuItem;
Label label1;

. . .
This would probably go into the OnLoad event of the page:
myMenuItem.NavigateUrl = "CandSeminar.aspx?key=" + Server.UrlEncode(label1.Text);

Note that Server.UrlEncode is necessary to put whatever is in label1.Text into a format suitable to be in a URL (for example, to encode the spaces to %20)
ANAT2403Author Commented:
The ID property does not exist in menuitem but I guess you ment the following:
            mpMenu.Items[1].NavigateUrl = "CandSeminar.aspx?key=" + Server.UrlEncode(mpLabel.Text);
The problem is that I can't put this line in the formload of the masterpage because in this time the controls are empty.
I thought of putting in in the event of MenuItemClick but I discovered that this event does not happen. No events take place in the master page and it goes directly to the page of the placeholder.
Where should I put it?
By the way should I write the word key in the navigateurl or I can write the word querystring?
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Ah, you're right. There is no ID property on the MenuItem, but it can be accessed exactly as you mentioned :)

I see -- so the value of mpLabel gets set during OnLoad of the page itself?

If that is the case, could you try putting this code into the OnPreRender event of the master page?

Article from MS:
Also, you don't have to use the word "key". It could be anything, querystring is good too.
ANAT2403Author Commented:
First I don't kmow how to invoke the preRender event of the masterpage.
Can you tell me?
Second I will explain my problem deeper:
In the pageload of the content page I read a value from the master page. I could read it ok until I made a
change in my program in which I call the content of the masterpage with the querystring.
The thing is that I call one content and then through a menu that is defined in the master page I move to another content. but now when I move from one content to another I loose the values of the master page.
I am not sure I explained it good.
The PreRender method will get invoked automatically by ASP.NET. You just need to add some handling for it. If you open the master page in design mode and press F4 (i.e. bring up the Properties window), and then click on the golden looking arrow, double click on PreRender to add a handler. Then you can type your code in there.

I see what you mean. So you browse to:
which works fine on the first page ... but as you click a link, the "querystring=Hello" part is lost? That is normal behavior, and you are trying to solve it in the correct way. Another way would be to try to set a cookie in the browser, but using the query string is a lot more straightforward.

Does mpLabel live on the master page?
ANAT2403Author Commented:
mplabel is on the master page.
I still have problems with invoking the prerender:
When I press the F4 for the properties in design mode of the master page, I see it deal with the DOCUMENT
and in this case ther is no golden looking arrow? how come?
ANAT2403Author Commented:
OK I almost solve all the problems here by using the event of the menu:
 Menu1_MenuItemClick(object sender, MenuEventArgs e) and give velue to:
e.Item.NavigateUrl = ..........
The only problem left is that I have to press the menuitem TWICE!!!!! in order to get the new choice. When I press the first time it goes to the MenuItemClick and set the new NavigateUrl but it does not switch to the new menuitem. Only
After I press the second tome it moves to the new choice. Why is that?
ANAT2403Author Commented:
I found the solution.
The solution is to use Response.Redirect.
I close the question.
Closed, 500 points refunded.
The Experts Exchange
Community Support Moderator of all Ages

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now