HT retrieve values in html table from codebehind

X

xzzy

I have hit a wall with not being able to enumerate the items collection in a
table row.

given:

<table runat=server id=table1>
<tr>
<td id=AAA>HowToReferenceThisValue</td>

etc...

from code behind, how can I iterate thru / parse the html table to get AAA's
text => 'HowToReferenceThisValue'
 
M

Muhammad Naveed Yaseen

HtmlTableCell cell = table1.Controls[0].Controls[0] as HtmlTableCell;

cell.InnerHtml would evaluate to 'HowToReferenceThisValue'
cell.ID would evaluate to server side id (AAA here)
cell.ClientID would evaluate to client-side id (which may be different
than server side depending on nesting level)

To access some html attribute other than id, use
cell.Attributes["NameOfAttribute"]

Iteration via IEnumerable would be like following (above mentioned
properties can be acessed after casting to HtmlTableRow or
HtmlTableCell if needed)

foreach (Control row in table1.Controls)
{
foreach (Control cell in control.Controls)
{
....
}
}
 
M

Muhammad Naveed Yaseen

HtmlTableCell cell = table1.Controls[0].Controls[0] as HtmlTableCell;

cell.InnerHtml would evaluate to 'HowToReferenceThisValue'
cell.ID would evaluate to server side id (AAA here)
cell.ClientID would evaluate to client-side id (which may be
different
than server side depending on nesting level)


To access some html attribute other than id, use
cell.Attributes["NameOfAttribute"]


Iteration via IEnumerable would be like following (above mentioned
properties can be acessed after casting to HtmlTableRow or
HtmlTableCell if needed)


foreach (Control row in table1.Controls)
{
foreach (Control cell in row.Controls)
{
....
}
}
 
X

xzzy

HtmlTableCell cell = table1.Controls[0].Controls[0] as HtmlTableCell;

cell.InnerHtml would evaluate to 'HowToReferenceThisValue'

++++++++++++++++++++++++++++++++++++++++++++++++++
Problem:
cell.InnerHtml is not a property of cell

further,
table1.Controls[0].Controls[0]
( System.Web.UI.HtmlControls.HtmlTableCell )
only lists the attributes, not the item
++++++++++++++++++++++++++++++++++++++++++++++++++

cell.ID would evaluate to server side id (AAA here)
cell.ClientID would evaluate to client-side id (which may be
different
than server side depending on nesting level)


To access some html attribute other than id, use
cell.Attributes["NameOfAttribute"]

Iteration via IEnumerable would be like following (above mentioned
properties can be acessed after casting to HtmlTableRow or
HtmlTableCell if needed)

++++++++++++++++++++++++++++++++++++++++++++++++++
Problem:
as far as I know, the items collection of
System.Web.UI.HtmlControls.HtmlTableCell is not enumerable
++++++++++++++++++++++++++++++++++++++++++++++++++

foreach (Control row in table1.Controls)
{
foreach (Control cell in row.Controls)
{
....
}
}
 
M

Muhammad Naveed Yaseen

++++++++++++++++++++++++++++++++++++++++++++++++++
Problem:
cell.InnerHtml is not a property of cell

http://msdn.microsoft.com/library/d...lshtmlcontainercontrolclassinnerhtmltopic.asp

And HtmlTableCell inherits from HtmlContainerControl.
So yes, InnerHtml is a property of cell (HtmlTableCell to which I had
cast variable 'cell').
further,
table1.Controls[0].Controls[0]
( System.Web.UI.HtmlControls.HtmlTableCell )
only lists the attributes, not the item

Attributes is an AttributesCollection
http://msdn.microsoft.com/library/d...mlcontrolshtmlcontrolclassattributestopic.asp
which implements Item
http://msdn.microsoft.com/library/d...fsystemwebuiattributecollectionclasstopic.asp
So yes, you can use Item (on Attributes of course).
Problem:
as far as I know, the items collection of
System.Web.UI.HtmlControls.HtmlTableCell is not enumerable

Problem :)
1) HtmlTableCell does not have items collection (as you yourself said
few lines ago).
2) Whichever object has Items, that is enumerable, for the type of
unit of items.
3) I had enumerated Controls collections (not Items), which is, yes,
also enumerable
http://msdn.microsoft.com/library/d...lrfsystemwebuicontrolcollectionclasstopic.asp

Why don't you please compile the code first and then comment.
I have compiled it and everything works as expected.
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top