samedi 9 mai 2015

How can I override the asp.net menu so that it doesn't litter the rendered ul with classes and styles?

I'll first start off with my question, following by context and the code I have so far.

Question: How can I override the asp.net menu control so that it produces a clean nested unordered list without the classes and styles?


Update (5/5/2015):

After refreshing the website, the earlier behavior of no menu appearing came back. So I'm guessing a few more things had to refresh as well.


While the asp:menu can be told to render as a nested UL, it litters the entire list with classes and styles and it interferes with my css menu system.

Through my research over the last two weeks, I learned:

  • How to create a custom class that inherits the asp.net menu
  • How to register that custom class in my masterpage so that I can add the custom control to the body

The default behavior of the custom control is the same as the standard asp:menu control, so I know that it successfully inherited the control.

What I have not been able to do yet is tell the custom control to ensure that no classes and styles are added to the unordered list elements.

I found the link below on Stackoverflow (in fact, that's what led me to the above skill development), but as written I was not able to achieve my objective.

Thank you for any insight you can provide. I have hit a wall here.

Now for the display of what I have so far:

I found this link at another forum (the relevant code pasted below and translated to VB): Disable javascript generation by ASP.NET menu control

This, I put into a "My_Custom_Class.vb" class file.

Imports Microsoft.VisualBasic

Namespace MCO

    Public Class MyCustomMenu
        Inherits System.Web.UI.WebControls.Menu

    Protected Overrides Sub OnPreRender(e As EventArgs)
        ' Don't call base OnPreRender
        ' MyBase.OnPreRender(e)
    End Sub
End Class

End Namespace

In my masterpage, I added this:

<%@ Register TagPrefix="mco" Namespace="MCO" %>

This enabled me to add the custom control to the body section.

<nav>                    
    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />                    
    <mco:MyCustomMenu ID="Menu1" runat="server" 
            DataSourceID="SiteMapDataSource1" 
            StaticDisplayLevels="2"
            StaticEnableDefaultPopOutImage="False"
            DynamicEnableDefaultPopOutImage="False" 
            IncludeStyleBlock="false"  
            RenderingMode="List">
    </mco:MyCustomMenu>
</nav>

Aucun commentaire:

Enregistrer un commentaire