I'm creating a live mockup for a client website, and I'm having some problems getting the design to work.
The site master has a menu that has multiple categories, but they all contain similar content. I need the menu to remember which one was selected. So I tried setting a cookie that would remember which category was last accessed, and leave that highlighted for the user. (I know this probably isn't the proper way to do it, but it's a mockup and I just want to get it done. Brute force is acceptable).
Here is my embarassingly bad code, inlined on the master page:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
switch (Path.GetFileNameWithoutExtension(HttpContext.Current.Request.Url.AbsolutePath).ToLower())
{
case "category1":
HttpContext.Current.Response.Cookies.Add(new HttpCookie("selectedCategory", "category1"));
break;
case "category2":
HttpContext.Current.Response.Cookies.Add(new HttpCookie("selectedCategory", "category2"));
break;
...
case "default":
HttpContext.Current.Response.Cookies.Add(new HttpCookie("selectedCategory", "category1"));
break;
}
}
// filePath will be "category1", "category2", etc.
public string IsSelected(string filePath)
{
return String.Compare(HttpContext.Current.Request.Cookies["selectedCategory"].Value, filePath, true) == 0
? " selected" // The class name to add to the CSS
: String.Empty;
}
And the links just call IsSelected
with the name "category1", "category2" etc.
<li class="menu<%=IsSelected("category1") %>"><a href="/Category1.aspx">
My problem is that the cookie is always a page behind. If I go from category1 to category2, category1 stays highlighted. If I continue to category3, now category2 is highlighted.
What I don't understand is that when I set breakpoints, the cookies value is being set to the proper page, but it's being read out wrong. Can I not do this at all?
Can anyone tell me what is wrong or a way to fix this?