views:

1043

answers:

3

How can I add a span tag from a code behind? Is there an equiv HtmlControl? I am currently doing it this way. I am building out rows to a table in an Itemplate implementation.

var headerCell = new TableHeaderCell { Width = Unit.Percentage(16)};
var span = new LiteralControl("<span class='nonExpense'>From<br/>Date</span>");
headerCell.Controls.Add(span);
headerRow.Cells.Add(headerCell);

I know I could use new Label(), but I am trying to avoid a server control here. Am I correct in using the LiteralControl this way? Does anyone have any better ideas of how to do this?

Thanks for any feedback, ~ck

A: 

new HtmlGenericControl("span")

Frank Schwieterman
+4  A: 

With HtmlGenericControl you can create a span dynamically like that :

var span = new HtmlGenericControl("span");
span.InnerHtml = "From<br/>Date";
span.Attributes["class"] = "nonExpense";
headerCell.Controls.Add(span);
Canavar
A: 

Following the idea that our friend Canavar said.

Look under System.Web.UI.HtmlControls namespace and you will see a whole bunch of HTML controls that have been mapped to objects, if you can use those. HtmlGenericControl fits in to any controls that are not defined in .NET and SPAN is a exemple of that.

Happy Coding.

Oakcool