views:

124

answers:

3

I am trying to make my site fully w3c validator compliant.

At the moment, I am getting an error because a table which is generated programatically and insterted into a label's text attribute shows as a table nested in a span tag.

e.g

MyPage.aspx.vb

strHtml = "<table><tr><td>Hello World</td></tr></table>" 
Me.myTable.Text = strHtml

MyPage.aspx

<asp:Label ID="myTable" runat="server" Text="testimonialTable"></asp:Label> 

Renders as:

<span id="ctl00_Main_myTable">
<table><tr><td>Hello World</td></tr></table>
</span>

When I then validate my page at validator.w3.org I get the following error:

document type does not allow element "table" here; missing one of "object", "applet", "map", "iframe", "button", "ins", "del" start-tag

The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element.

I assume this is because I'm trying to put a block element (table) inside an inline element (span) - but I don't know how else to do this!

Anyone got any idea of a workaround?

Thanks,

Ben

A: 

Use a Literal instead of a Label to avoid the wrapping <span> element:

<asp:Literal ID="myTable runat="server" />
Jørn Schou-Rode
You're a legend! (Out of interest, is this best practice to use Literals instead of Labels? - Sorry, bit of a newb!)
It depends on what you are doing. `Label` renders a `<span>` or a `<label>` element which can be styled using properties on the control. `Literal` renders only the value of its `Text` property, and provides no means of styling. For injection of HTML into the object model, `Literal` works well. However, injecting HTML does somewhat break with the ASP.NET control abstraction, and you might want to consider if your table can be built using `GridView` or `Table` instead.
Jørn Schou-Rode
A: 

Use a panel and add a generichtml control to it.

Dustin Laine
A: 

Why not just use a Table control:

<asp:Table ID="myTable" runat="server" />

Then simply create the columns and rows and add them to the table server side.

tvanfosson