Hi All!
(sorry for the code mess, I still can't master this editor! )
Here is the scenario:
I have several Menu controls in a webform and I want to dynamically change the style of a given menu item on selecting / desesecting it.
The menu is like this:
<asp:Menu ID="Menu1" runat="server" CssSelectorClass="DashboardMenu" Orientation="Horizontal" OnMenuItemClick="MenuItemClick">
<Items>
<asp:MenuItem Value="0" Text="0" />
<asp:MenuItem Value="1" Text="1" />
<asp:MenuItem Value="2" Text="2" />
<asp:MenuItem Value="3" Text="3" />
</Items>
</asp:Menu>
I slightly changed the MenuAdapter BuildItem Sub code to look like this:
If ((Not IsNothing(menu)) AndAlso (Not IsNothing(item)) AndAlso (Not IsNothing(writer))) Then writer.WriteLine()
writer.WriteBeginTag("li")
If item.Selected = False Then
writer.WriteAttribute("class", IIf(item.ChildItems.Count > 0, "AspNet-Menu-WithChildren", "AspNet-Menu-Leaf"))
Else
writer.WriteAttribute("class", IIf(item.ChildItems.Count > 0, "AspNet-Menu-WithChildren-Selected", "AspNet-Menu-Leaf-Selected"))
End If
writer.Write(HtmlTextWriter.TagRightChar)
writer.Indent = writer.Indent + 1
writer.WriteLine()
If (item.NavigateUrl.Length > 0 Or item.Selectable = True) Then
writer.WriteBeginTag("a")
If item.NavigateUrl.Length > 0 = True Then
writer.WriteAttribute("href", Page.ResolveUrl(item.NavigateUrl))
Else
writer.WriteAttribute("href", Page.ClientScript.GetPostBackClientHyperlink(menu, "b" + item.ValuePath.Replace(menu.PathSeparator.ToString(), "\\"), True))
End If
If item.Selected = False Then
writer.WriteAttribute("class", "AspNet-Menu-Link")
Else
writer.WriteAttribute("class", "AspNet-Menu-Link-Selected")
End If
If (item.Target.Length > 0) Then
writer.WriteAttribute("target", item.Target)
End If
So, what I wanted to achieve doing this is: based on the Selected flag, I want to apply a different class to the MenuItem “li” and “a” elements, to control them through css later, like this:
.DashboardMenu .AspNet-Menu-Horizontal ul.AspNet-Menu li.AspNet-Menu-Leaf
{
background-color: #F7F7F7; }
.DashboardMenu .AspNet-Menu-Horizontal ul.AspNet-Menu li.AspNet-Menu-Leaf-Selected
{
background-color: #003B72; }
.DashboardMenu .AspNet-Menu-Horizontal ul.AspNet-Menu li.AspNet-Menu-Leaf a.AspNet-Menu-Link
{
color: #003B72!important;
}
.DashboardMenu .AspNet-Menu-Horizontal ul.AspNet-Menu li.AspNet-Menu-Leaf-Selected a.AspNet-Menu-Link-Selected
{
color: White!important;
}
If I explicitly set one of the items Selected property to “true” I get the right results, but, as soon I try to change it using the “MenuItemClick” event, I don’t get the right Css class applied, even when the MenuAdapter code is called on the postback event.
This is the HTML generated code with the selected=True property assigned throught the MenuItemClick event:
<div class="DashboardMenu">
<div class="AspNet-Menu-Horizontal">
<ul class="AspNet-Menu">
<li class="AspNet-Menu-Leaf">
<a href="javascript:__doPostBack('ctl00$ProfilerPlaceHolder$Menu1','b0')" class="AspNet-Menu-Link">
0
</a>
</li>
<li class="AspNet-Menu-Leaf">
<a href="javascript:__doPostBack('ctl00$ProfilerPlaceHolder$Menu1','b1')" class="AspNet-Menu-Link">
1
</a>
</li>
<li class="AspNet-Menu-Leaf">
<a href="javascript:__doPostBack('ctl00$ProfilerPlaceHolder$Menu1','b2')" class="AspNet-Menu-Link">
2
</a>
</li>
<li class="AspNet-Menu-Leaf">
<a href="javascript:__doPostBack('ctl00$ProfilerPlaceHolder$Menu1','b3')" class="AspNet-Menu-Link">
3
</a>
</li>
</ul>
</div>
</div>
And here is the code generated if I set that property beforehand in the Menu properties, which is the result I want to achieve programatically
<asp:Menu ID="Menu1" runat="server" CssSelectorClass="DashboardMenu" Orientation="Horizontal" OnMenuItemClick="MenuItemClick">
<Items>
<asp:MenuItem Value="0" Text="0" Selected="True" />
<asp:MenuItem Value="1" Text="1" />
<asp:MenuItem Value="2" Text="2" />
<asp:MenuItem Value="3" Text="3" />
</Items>
</asp:Menu>
<div class="DashboardMenu">
<div class="AspNet-Menu-Horizontal">
<ul class="AspNet-Menu">
<li class="AspNet-Menu-Leaf-Selected">
<a href="javascript:__doPostBack('ctl00$ProfilerPlaceHolder$Menu1','b0')" class="AspNet-Menu-Link-Selected">
0
</a>
</li>
<li class="AspNet-Menu-Leaf">
<a href="javascript:__doPostBack('ctl00$ProfilerPlaceHolder$Menu1','b1')" class="AspNet-Menu-Link">
1
</a>
</li>
<li class="AspNet-Menu-Leaf">
<a href="javascript:__doPostBack('ctl00$ProfilerPlaceHolder$Menu1','b2')" class="AspNet-Menu-Link">
2
</a>
</li>
<li class="AspNet-Menu-Leaf">
<a href="javascript:__doPostBack('ctl00$ProfilerPlaceHolder$Menu1','b3')" class="AspNet-Menu-Link">
3
</a>
</li>
</ul>
</div>
</div>
Any ideas on what i'm missing here?
Thanks,
Juan
Juan Barrera
MCTS
Please remember to click "Mark as Answer" on this post if it helped you.