Tuesday, 18 September 2007

Static Selected Style (StaticSelectedStyle) property of ASP.NET Menu Control

Static Selected Style (StaticSelectedStyle) property of ASP.NET Menu Control
When you specify style f0r this property in design time it doesn't work.
You have to work arround a little and write code using c# or vb.net.
I found code for Vb.net and changed it to c#.
The guy was using vb.net code onload event of page. I tried to use my code on page load event but it didn't work
so I am using Data Bound event of menu control.

protected void Menu1_DataBound(object sender, EventArgs e)
{

// Get page name from relative path


string ThisPage = Page.AppRelativeVirtualPath;
int SlashPos = ThisPage.IndexOf("/");
string PageName = Kamboh.MySqlBusinessLogicLayer.Common.Utilities.Right(ThisPage, ThisPage.Length - SlashPos-1);


// Select menu item with matching NavigateUrl property


foreach (MenuItem parentmenu in Menu1.Items)
{
if (parentmenu.NavigateUrl.ToLower() == PageName.ToLower())
{
parentmenu.Selected = true;
break;
}
else

foreach (MenuItem ChildMenu in parentmenu.ChildItems)
{
if (ChildMenu.NavigateUrl == PageName)
{
ChildMenu.Selected = true;
break;
}
}
}


ToLowere() is used to match the name as my values are coming from xml file.
You can replace this line Kamboh.MySqlBusinessLogicLayer.Common.Utilities.Right
with following function
public static string Right(string param, int length)
{
//start at the index based on the lenght of the sting minus
//the specified lenght and assign it a variable
string result = param.Substring(param.Length - length, length);
//return the result of the operation
return result;
}

No comments: