How to assign csssytle attribute to html elements through code?

  • Thread starter Thread starter Zdenko Rupcic
  • Start date Start date
Z

Zdenko Rupcic

How to programatically add cssStyle property to HtmlTableCell (or any other control)? I need to create and populate HtmlTableCell at run-time (because some requirements need to be met for the cell to be displayed) and I can't use System.Web.UI.WebControls.TableCell instead because I'm placing the cell in a HtmlTable.

Code goes like this:

Dim newrow As New HtmlTableRow
Dim newcell As New HtmlTableCell
newcell.ColSpan = 4
newcell.Attributes.CssStyle.Value = "tablecell" 'defined inside css file
newcell.InnerHtml = "<a href=""new.aspx"">new</a>" 'link to display in a cell
newrow.Cells.Add(newcell)
tblMain.Rows.Add(newrow) 'tblMain is html table

This creates the cell correctly (including colspan attribute and link contained within), but will not assign cssStyle attribute to it. Page source shows this:

<tr>
<td colspan="4" style="tablecell"><a href="new.aspx">new</a></td>
</tr>

Obviously, there should be "class" attribute instead of "style".
 
Hi,
You add the class attribute as follows

newcell.Attributes.Add("class","tablecell")

Regards,
Mohamed Mosalem
 
Mohamed said:
Hi,
You add the class attribute as follows

newcell.Attributes.Add("class","tablecell")

Regards,
Mohamed Mosalem

Excellent, I see this can have more uses than only assigning css class - thanks!
 
Back
Top