ASP.Net 4.0 and tableless menu control
One of the issues I really like to read and learn is client side technologies. I am an ASP.Net guy at heart but I find CSS particular useful and I have been blogging about CSS in my other blog.
Have a look here if you want for some interesting posts on CSS.
I believe designing web applications and sites according to web standards. I do not think designing our websites with tables is correct.
Tables should be used for what they are good at doing, Display tabular data .
So if we are in charge of our web template we should use Divs and an external CSS file to style the main areas of our template.
When it comes to web server controls we have the issue of not being in charge of the HTML that is emitted from the asp.net engine.
In asp.net 4.0, we have more control of the HTML that is produced from the asp.net engine.
Let's see a little example by using the ASP.Net menu web server control and how it renders its HTML in ASP.Net 4.0.
To follow along with this example you must have .Net framework and VS 2010 RC installed in your machine.
1) Launch VS 2010
2) Create a new web site by selecting ASP.Net website from the templates
3) Choose C# or VB as the development language.
4) Save your site with an appropriate filename in your local filesystem
5) Create an ASP.Net menu control on the default.aspx. Change the RenderingMode to Table. This is a new property in ASP.Net 4.0. What this makes is to instruct the asp.net engine to render HTML code for the menu control the old way.
Your code should be like this
<asp:Menu runat="server" ID="mymenu" RenderingMode="Table">
<Items>
<asp:MenuItem Text="Orders" Value="orders">
</asp:MenuItem>
<asp:MenuItem Text="Sales" Value="Sales">
</asp:MenuItem>
<asp:MenuItem Text="Customers" Value="Customers">
</asp:MenuItem>
<asp:MenuItem Text="Employees" Value="Employees">
</asp:MenuItem>
</Items>
</asp:Menu>
6) Save your application and run it. Go to View->Source in the IE browser menu and see the HTML emitted.
It should be something like this. So it is styled like a table.
<table id="MainContent_mymenu" class="MainContent_mymenu_2" cellpadding="0" cellspacing="0" border="0">
<tr onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(this)" id="MainContent_mymenun0">
<td><table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;width:100%;"><a class="MainContent_mymenu_1" href="javascript:__doPostBack('ctl00$MainContent$mymenu','orders')">Orders</a></td>
</tr>
</table></td>
</tr><tr onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(this)" id="MainContent_mymenun1">
<td><table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;width:100%;"><a class="MainContent_mymenu_1" href="javascript:__doPostBack('ctl00$MainContent$mymenu','Sales')">Sales</a></td>
</tr>
</table></td>
</tr><tr onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(this)" id="MainContent_mymenun2">
<td><table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;width:100%;"><a class="MainContent_mymenu_1" href="javascript:__doPostBack('ctl00$MainContent$mymenu','Customers')">Customers</a></td>
</tr>
</table></td>
</tr><tr onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(this)" id="MainContent_mymenun3">
<td><table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;width:100%;"><a class="MainContent_mymenu_1" href="javascript:__doPostBack('ctl00$MainContent$mymenu','Employees')">Employees</a></td>
</tr>
</table></td>
</tr>
</table>
7) Change the RenderingMode to List, and build again your site.Go to View->Source in the IE browser menu and see the HTML emitted.
Youw will not see any tables. In its place you will see a list.Here it is.
<ul class="level1">
<li><a class="level1" href="http://weblogs.asp.net/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts§ionid=1153&postid=7430143#" onclick="__doPostBack('ctl00$MainContent$mymenu','orders')">Orders</a></li>
<li><a class="level1" href="http://weblogs.asp.net/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts§ionid=1153&postid=7430143#" onclick="__doPostBack('ctl00$MainContent$mymenu','Sales')">Sales</a></li>
<li><a class="level1" href="http://weblogs.asp.net/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts§ionid=1153&postid=7430143#" onclick="__doPostBack('ctl00$MainContent$mymenu','Customers')">Customers</a></li>
<li><a class="level1" href="http://weblogs.asp.net/controlpanel/blogs/posteditor.aspx?SelectedNavItem=Posts§ionid=1153&postid=7430143#" onclick="__doPostBack('ctl00$MainContent$mymenu','Employees')">Employees</a></li>
</ul>
It is so much easier to style menus with CSS when we have ul,li elements as the HTML semantic.
To see how to do that have a look here
Hope that helps