Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

Removing SPAN-tags around server control

I had to write some ASP.NET server controls for our current SharePoint portal project. We have very nice DIV-based layout and using standard components that generate table and a lot of JavaScript seems to me like an bad idea. I found out that server controls put container tags around their mark-up. I needed my own tags around output and I found a way how to achieve it.

 

NB! This blog is moved to gunnarpeipman.com

Click here to go to article

Comments

Gilbes said:

Or override the TagKey property (I forget which property to override if its a non-standard HTML element), and possibly AddAttributesToRender if you need to.

# April 9, 2009 7:33 PM

Simon said:

You can also inherit from Control instead of WebControl/Composite control to avoid your control rendering surrounding divs or spans.

# September 9, 2009 10:39 PM

Daniel Hanson said:

Where do these go?

# October 30, 2009 4:27 PM

musch said:

spanx dude - solved my headache :)

# June 16, 2010 8:42 AM

Katie said:

This saved me some time today, so thank you very much, helpful article.

# July 13, 2011 1:28 PM

jj819 said:

Hi,

I would like to know where you add these two functions? In the .aspx.cs page or the .aspx page?

# November 10, 2011 3:50 PM

DigiMortal said:

This code goes to class file of server control.

# November 10, 2011 5:22 PM

Pedro Checkos said:

An other way to do it:

public class CustomCheckBox : CheckBox

{

protected override void Render(HtmlTextWriter writer)

{

// Use custom writer

writer = new HtmlTextWriterNoSpan(writer);

// Call base class

base.Render(writer);

}

}

public class HtmlTextWriterNoSpan : HtmlTextWriter

{

public HtmlTextWriterNoSpan(TextWriter textWriter)

: base(textWriter)

{ }

protected override bool OnTagRender(string name, HtmlTextWriterTag key)

{

// Do not render <span> tags

if (key == HtmlTextWriterTag.Span)

return false;

// Otherwise, call the base class (alway true)

return base.OnTagRender(name, key);

}

}

# January 25, 2013 8:00 PM