Microsoft has been touting ASP.Net 2.0 as taking great strides towards XHTML compatibility and meeting common accessibility guidelines. However, some controls still add table
tags around them, despite your best efforts to avoid using tables. The new Login control is one of those.
For me, the table wrapper around the Login control was causing rendering problems. I was using a pure CSS layout with no tables for the login form, and the presence of the table tag wrapped around the control was breaking my layout. A quick Google search (q=asp.net+2.0+login+control+table) found a blog post that outlined a solution.
The fix was simple: create a new class, inherit from the existing System.Web.UI.WebControls.Login
class, and override the Render method. A few lines of code, and the table wrapper was replaced with a div wrapper. There was one catch: the version posted on the aforementioned blog didn’t include the CssClass
attribute on the new control. One extra line of code fixed that.
Below is the code, all but one line thanks to Alex Gorbatchev of dreamprojections.com:
public class CssLogin : System.Web.UI.WebControls.Login { protected override void Render( HtmlTextWriter writer ) { WebControl div = new WebControl( HtmlTextWriterTag.Div ); LayoutTemplate.InstantiateIn( div ); Controls.Clear(); Controls.Add( div ); div.CopyBaseAttributes( this ); div.CssClass = this.CssClass; div.RenderControl( writer ); } }