Removing the TABLE from the CreateUserWizard control

About two months ago, I wrote a post about removing the TABLE from ASP.Net 2.0’s Login control. The below code will let you do the same from the CreateUserWizard control. One important caveat: for this to work (in my limited testing), you must provide a custom template for the ContentTemplate and CustomNavigationTemplate of the CreateUserWizardStep, and for the ContentTemplate of the CompleteWizardStep.

public class CssCreateUserWizard : System.Web.UI.WebControls.CreateUserWizard
{
    protected override void Render( HtmlTextWriter writer )
    {
        if ( CreateUserStep.ContentTemplate != null && this.ActiveStep == this.CreateUserStep )
        {
            WebControl creatediv = new WebControl( HtmlTextWriterTag.Div );
            creatediv.CssClass = this.CssClass;
            CreateUserStep.ContentTemplate.InstantiateIn( creatediv );
            CreateUserStep.ContentTemplateContainer.Controls.Clear();
            CreateUserStep.ContentTemplateContainer.Controls.Add( creatediv );
            creatediv.RenderControl( writer );

            if ( CreateUserStep.CustomNavigationTemplate != null )
            {
                WebControl navdiv = new WebControl(HtmlTextWriterTag.Div);
                navdiv.CssClass = this.CssClass;
                CreateUserStep.CustomNavigationTemplate.InstantiateIn(navdiv);
                CreateUserStep.CustomNavigationTemplateContainer.Controls.Clear();
                CreateUserStep.CustomNavigationTemplateContainer.Controls.Add(navdiv);
                navdiv.RenderControl(writer);
            }
        }

        if ( CompleteStep.ContentTemplate != null && this.ActiveStep == this.CompleteStep )
        {
            WebControl completediv = new WebControl( HtmlTextWriterTag.Div );
            completediv.CssClass = this.CssClass;
            CompleteStep.ContentTemplate.InstantiateIn( completediv );
            CompleteStep.ContentTemplateContainer.Controls.Clear();
            CompleteStep.ContentTemplateContainer.Controls.Add( completediv );
            completediv.RenderControl( writer );
        }
    }
}

0 thoughts on “Removing the TABLE from the CreateUserWizard control

  • Hey, this is a great article. I’m looking to do this exact thing. It’s been a while since you wrote this article so I’m wondering if you’ve had any more insights.

    I using this code but when I do, nothing shows up. I did add the customtemplate and CustomNavigationTemplate (with nothing in them). Is that sufficient? Also, is the CompleteWizardStep node required?

    Thanks in advance.

  • I would try the following, which I’ve used rather successfully on quite a few sites. Note that this will strip any HTML TABLEs from your markup, so don’t use any in your templates.

    public class CssCreateUserWizard : System.Web.UI.WebControls.CreateUserWizard
    {
    	protected override void Render( HtmlTextWriter writer )
    	{
    		//write opening div
    		writer.WriteBeginTag("div");
    		writer.WriteAttribute("class", this.CssClass);
    		writer.Write(HtmlTextWriter.TagRightChar);
    
    		//get the rendered HTML
    		StringBuilder sb = new StringBuilder();
    		StringWriter sw = new StringWriter(sb);
    		HtmlTextWriter hw = new HtmlTextWriter(sw);
    
    		//render output to string
    		base.RenderContents(hw);
    
    		//remove tables
    		string str = sb.ToString();
    		str = Regex.Replace(str, "</?table[^>]*>", String.Empty);
    		str = Regex.Replace(str, "</?tr[^>]*>", String.Empty);
    		str = Regex.Replace(str, "</?td[^>]*>", String.Empty);
    		str = Regex.Replace(str, "</?thead[^>]*>", String.Empty);
    		str = Regex.Replace(str, "</?tbody[^>]*>", String.Empty);
    
    		writer.Write(str);
    
    		//write closing div
    		writer.WriteEndTag("div");
    	}
    }
    
  • I’m no regex expert, but, the remove tables seems to do the same thing over and over again. It doesn’t remove the table, but it does remove the closing tags within my wizard and this causes a world of problems. I’m assuming the regex stuff should be more related to “table tag” stuff like table, tr, td, th, tbody. ? Am I on the right track?

  • Actually… I think I got it. I changed the regex stuff to this:
    str = Regex.Replace(str, “]*>|]*>|]*>|]*>|]*>”, String.Empty);

    Seems to work great now! Thanks for all the help.

  • oooooh interesting… I just pasted my code and it appears your parser is removed the valuable syntax. I see the problem now. I’ll past it again with a space between each char. This will only be the actual regex string.

    ] * > | ] * > | ] * > | ] * > | ] * >

  • dang… that’s sneaky… I couldn’t even do it like that.. I’ll try replacing the code with html safe values. If this doesn’t work, I’ll stop comment spamming:

    </?table[^>]*>|</?tr[^>]*>|</?td[^>]*>|</?thead[^>]*>|</?tbody[^>]*>

  • The regex is now correct in my previous comment. Basically, it strips out TABLE, THEAD, TBODY, TR, and TD tags, which are those created by the CreateUserWizard adapter. You can use the same regex patterns with TFOOT, COLGROUP, COL, and TH to be more thorough (though it’s unnecessary as they are not used).

Leave a 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.