473,383 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to assign csssytle attribute to html elements through code?

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".
Sep 25 '06 #1
2 8312
Hi,
You add the class attribute as follows

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

Regards,
Mohamed Mosalem

"Zdenko Rupcic" wrote:
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".
Sep 25 '06 #2
Mohamed Mosalem wrote:
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!
Sep 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: mickjames | last post by:
Hi, I'd like to include the whole web page content (as opposed to just the headlines) into RSS/XML to enable people to read them via rss feed readers. Question: how to convert HTML elements...
22
by: Luke | last post by:
Elements with name attribute: form, input, textarea, a, frame, iframe, button, select, map, meta, applet, object, param, img (if you know more reply...) Methods of addresing html elements:...
2
by: Linus Martinsson | last post by:
How can I change attribute in html elements from my aspx.cs page? For example if I want to change the src-attribute in an iframe. //Linus
5
by: Urs Vogel | last post by:
Hi how do I add HTML elements dynamically to my page? Is there some similar way like with ASPX controls, or do I have to render them out in a custom control? Thanks, Urs
3
by: Rudi Hausmann | last post by:
Hi! I create HTML pages on the server side. I want to extend DIV elements with more information. E.g.: <DIV id="Joe" /> Now I want Joe to have an age for example and access it using:
3
by: kumaryindu | last post by:
Hi, I want to parse HTML code,so that I can acess html elements. For Ex: <td class="tim"><a target="_new" href="/stocks/marketstats/indcomp.php?optex=NSE&indcode=Banks - Private Sector">Banks...
1
by: Florian Paulus | last post by:
Hi group, i'm wondering how to assign an attribute to a field that a class inherited from a base class. Say base class has a field protected string message, so in the derived class i want to...
7
by: chemlight | last post by:
I am having an issue with HTML elements not printing when positioned absolutely when they extend beyond the first page. I am working on some foreign tax refund forms. The forms are cut into multiple...
0
by: petrucino | last post by:
hi all..sorry if my english so bad.. i've learn about parsing..but it was not enough..i've make a code to getting value of attribute from "http://www.codeproject.com/KB/cs/htmlparser.aspx" and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.