Wednesday, October 24, 2007

Relative path and menu highlight

One of the beatiful thing about Asp.Net is you can use ~ to make a relative path, and .NET will figure out the full path at the run time no matter what environment the application sits on. (For example, your app might not be on the root folder on the QA box as it in production). If you use .NET built-in menu the highlight functions is alreay there. meaning the a menu item can be highlighted if you are on this target page. However, if you are building a menu by yourself, you have to implement it by yourself, and this is how you can do it:
(1) in the menu source file, normally a xml file, you put the app relative path with ~ in it, for example use ~/myfolder/mypage.aspx instead of /myfolder/mypage.aspx
(2) in the load event of the menu control, whether custom or user control, check the if current Page.AppRelVirtualPath is equal to a particular menu item, if so highlight that menu item

that's it

No comments: