Removing tables in Microsoft’s ASP.Net 2.0 Controls

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 );
        }
    }

0 thoughts on “Removing tables in Microsoft’s ASP.Net 2.0 Controls

  • Hey, I’m still using your blog/comments on demarzo.net – and you MUST have just started redirecting it. Can you take the redirect back out, or make sure all content is rolled forward? I’m desperately trying to get table tags ripped out of the stupid login control.

    Thanks!

  • Everything that was on my old blog (demarzo.net) should be on this blog — with the same original dates, post titles, and content. The old blog is long gone (it’s been redirecting to this place for many months).

  • Brian,

    Thanks for your help before. I had to let this sit on the back burner while I built up my VB.NET and ASP.NET skills. When I first came by, I was about 6 weeks into .NET programming.

    I figured out how to implement this after surfing MSDN a bit today, and used your source code as-is (translated to VB.NET).

    Just wanted to say thanks!

Leave a Reply to Dirar Cancel Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.