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?
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);
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);
new HtmlGenericControl("span")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With