Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

CSS Friendly Menu Control in ASP.NET 4.0

4.33/5 (9 votes)
18 May 2010CPOL2 min read 66.6K  
CSS Friendly Menu Control in ASP.NET 4.0

It is much easier to apply CSS when we have ul,li elements as the HTML content. If we look into ASP.NET Menu Control till Version 3.5, it is rendered as Table-TR-TD Tag. Though Table/Tr/Td is quite useful to display tabular data, it sometimes creates a big problem when we need to do more work with CSS. To overcome this problem, we generally used CSS Friendly adapter to render the ASP.NET Control in ul/li mode.

ASP.NET 4.0 makes things easier for web developers by providing “RenderingMode” properties. Here we can specify RenderMode of a ASP.NET Menu control which define what will be the HTML Render Content Type. By default, the mode is “List” which means control will be render as ul/li.

As per the above diagram, we can see that there are three modes available. We can use any one of them as per the requirement.

Let’s see one small example by using the ASP.NET menu web control and check how it renders as HTML in ASP.NET 4.0. Assume that we are having the following piece of code:

ASP.NET
<asp:Menu runat="server" ID="customeMenu" RenderingMode="List" >
           <Items>
               <asp:MenuItem Text="File" Value="File"></asp:MenuItem>
               <asp:MenuItem Text="Edit" Value="Edit"></asp:MenuItem>
               <asp:MenuItem Text="View" Value="View"></asp:MenuItem>
               <asp:MenuItem Text="WebSite" Value="WebSite"></asp:MenuItem>
           </Items>
       </asp:Menu>

This Menu control will be rendered as below HTML code in ASP.NET 4.0 as, we have mentioned “List” as the rendering mode.

HTML
<ul>
		<li><a  href="#">File</a></li>
		<li><a  href="#">Edit</a></li>
		<li><a  href="#">View</a></li>
		<li><a  href="#">WebSite</a></li>
	</ul>

To test the above scenario, run the web application that contains the menu control and after page rendered is complete, Right Click on view source of the aspx page. The output will appear as below:

We can also generate the Menu Control as HTML Table like the earlier version by using RenderingMode=”Table”.

So, for the same block of code,

ASP.NET
<asp:Menu runat="server" ID="customeMenu" RenderingMode="Table" >
           <Items>
               <asp:MenuItem Text="File" Value="File"></asp:MenuItem>
               <asp:MenuItem Text="Edit" Value="Edit"></asp:MenuItem>
               <asp:MenuItem Text="View" Value="View"></asp:MenuItem>
               <asp:MenuItem Text="WebSite" Value="WebSite"></asp:MenuItem>
           </Items>
       </asp:Menu>

HTML Rendered output will be as follows:

HTML
<table >
	<tr >
		<td><table>
			<tr>
				<td >File</a></td>
			</tr>
		</table></td>
	</tr><tr>
		<td><table >
			<tr>
				<td>Edit</a></td>
			</tr>
		</table></td>
	</tr><tr>
		<td><table >
			<tr>
				<td >View</a></td>
			</tr>
		</table></td>
	</tr><tr >
		<td><table >
			<tr>
				<td>WebSite</a></td>
			</tr>
		</table></td>
	</tr>
</table>

Note: With the above HTML content ASP.NET engine automatically adds few client side script along like onmouseover=”Menu_HoverStatic(this)” onmouseout=”Menu_Unhover(this)” onkeyup=”Menu_Key(this)” id=”customeMenun0? with rendered menu items which helps the developer to handle the client side events.


kick it on DotNetKicks.com

Shout it


Filed under: .NET 4.0, ASP.NET, ASP.NET 4.0, Visual Studio 2010

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)