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 );
        }
    }
}
Bookmark and Share

Popularity: 21% [?]

Related Posts

You don\'t become first by hiring second-best.

-- Brian DeMarzo

Comments 8

  1. Kevin wrote:

    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.

    Posted 14 Nov 2007 at 1:14 pm
  2. brian wrote:

    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”);
    	}
    }
    
    Posted 14 Nov 2007 at 4:13 pm
  3. Kevin wrote:

    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?

    Posted 14 Nov 2007 at 4:55 pm
  4. brian wrote:

    Text got stripped out when the post was made, let me fix it…

    OK, should be better now. Damn web editors!

    Posted 14 Nov 2007 at 4:56 pm
  5. Kevin wrote:

    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.

    Posted 14 Nov 2007 at 5:00 pm
  6. Kevin wrote:

    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.

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

    Posted 14 Nov 2007 at 5:02 pm
  7. Kevin wrote:

    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[^>]*>

    Posted 14 Nov 2007 at 5:04 pm
  8. brian wrote:

    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).

    Posted 14 Nov 2007 at 5:12 pm

Post a Comment

Your email is never published nor shared. Required fields are marked *