The SharePoint Team has released the source code to the SharePoint menu class. (MossMenu). I have modified the source to make the selected item stay selected during a postback.
The solution is a modification of a blog entry in Itay Shakury's blog.
In my sites I do not have menu items pointing to specific views in lists etc. (If I change the default view the menu item still shows the default view) So SharePoint will rewrite my url.
Ex: SharePoint will “UrlRewrite” the following url
http://server/site/Listname/
into
http://server/site/Listname/Forms/My%20Default.aspx
Another example of UrlRewrite is when you specify a folder in a document list. I will not provide a full example url because it contains a lot of characters:
http://server/site/listname/Forms/My%20DefaultView.aspx?RootFolder=http%3a%2f%2fsite...
The good part is that the parameter RootFolder (in my case) is the same as I specified in my menu item. So instead I compare the value of the parameter
Here is the code to handle the UrlRewritings:
(See Itay Shakury's blog on where to insert the code, you must modify the OnMenuItemDataBound function)
if (Page.Request.Url != null)
{
String UrlToCheck = Page.Request.Url.ToString().ToLower();
String UrlOnItem = e.Item.NavigateUrl.ToLower();
String UrlPathParam = String.IsNullOrEmpty(Page.Request["rootfolder"]) ? "" : Page.Request["rootfolder"].ToLower();
Boolean ItemSelected = false;
UrlOnItem = UrlOnItem.Replace(SPContext.Current.Site.RootWeb.Url, "");
UrlToCheck = UrlToCheck.Replace(SPContext.Current.Site.RootWeb.Url, "");
if (UrlPathParam.Length != 0)
UrlPathParam = UrlPathParam.Replace(SPContext.Current.Site.RootWeb.Url, "");
UrlOnItem = Page.Server.UrlDecode(UrlOnItem);
UrlToCheck = Page.Server.UrlDecode(UrlToCheck);
if (UrlPathParam.Length != 0)
UrlPathParam = Page.Server.UrlDecode(UrlPathParam);
if (UrlToCheck == UrlOnItem UrlOnItem == UrlPathParam)
{
ItemSelected = true;
}
else if (UrlToCheck.Contains("/forms/")) // Check if the url is without the forms/pagename
{
UrlToCheck = UrlToCheck.Substring(0, UrlToCheck.IndexOf("/forms/") + 1);
if (UrlToCheck == UrlOnItem)
{
ItemSelected = true;
}
}
e.Item.Selected = ItemSelected;
}
1 comment:
Should this:
UrlToCheck == UrlOnItem UrlOnItem == UrlPathParam
be this:
UrlToCheck == UrlOnItem || UrlOnItem == UrlPathParam
or:
UrlToCheck == UrlOnItem && UrlOnItem == UrlPathParam
Post a Comment