How to assign csssytle attribute to html elements through code?

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".
 
G

Guest

Hi,
You add the class attribute as follows

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

Regards,
Mohamed Mosalem
 
Z

Zdenko Rupcic

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,005
Messages
2,570,264
Members
46,859
Latest member
HeidiAtkin

Latest Threads

Top