Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add a span tag, not a Label control?

How can I add a span tag from a code behind? Is there an equivalent 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?

like image 973
Hcabnettek Avatar asked Nov 25 '09 18:11

Hcabnettek


4 Answers

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);
like image 123
Canavar Avatar answered Nov 11 '22 22:11

Canavar


Label span = new Label();
span.Text = "From<br/>Date";
span.CssClass = "nonExpense";
headerCell.Controls.Add(span);

Or, alternatively:

Label span = new Label {Text = "From<br/>Date", CssClass = "nonExpense"};
headerCell.Controls.Add(span);
like image 32
birdus Avatar answered Nov 11 '22 22:11

birdus


new HtmlGenericControl("span")
like image 2
Frank Schwieterman Avatar answered Nov 11 '22 21:11

Frank Schwieterman


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.

like image 1
Oakcool Avatar answered Nov 11 '22 21:11

Oakcool