tags:

views:

121

answers:

6

Hi Everyone, I need to extract some text from a HTML table

I tried using

tblGridHeader.Rows[0].InnerText.ToString()

But I'm getting the error of

"HTMLTableRow" does not support InnerText property.

I also tried InnerHTML, and still no go.

I did try using the cells property, but I'm getting an error of

Specified argument was out of the range of valid values.

Note: I just tried the cells property on a static table and it worked. So I guess it's something to do with my table being dynamically populated?

Code from visual studio editor:

<div id="divGridHeader" runat="Server" style="width:771px; text-align:left; overflow:hidden; float:left">
            <table cellpadding="0" cellspacing="0" border="0" id="tblGridHeader" runat="Server">
                <tr id="trMonth" runat="Server" class="fixedHeader">
                </tr>   
                <tr id="trDaysOfWeek" runat="Server" class="fixedHeader">                    
                </tr>   
                <tr id="trDaysInMonth" runat="Server" class="fixedHeader">                    
                </tr>
                <tr id="trFteLimit" runat="Server" class="fixedHeader" style="color:Black">                    
                </tr>                                             
            </table>
        </div> 

Code from browser:

<div id="ctl00_cphWorkforceScheduler_divGridHeader" style="overflow: hidden; width: 771px; text-align: left; float: left;">
<table id="ctl00_cphWorkforceScheduler_tblGridHeader" cellspacing="0" cellpadding="0" border="0" width="1085">
<tbody>
<tr id="ctl00_cphWorkforceScheduler_trMonth" class="fixedHeader">
</tr>
<tr id="ctl00_cphWorkforceScheduler_trDaysOfWeek" class="fixedHeader">
</tr>
<tr id="ctl00_cphWorkforceScheduler_trDaysInMonth" class="fixedHeader">
<th id="ctl00_cphWorkforceScheduler_thDay_10012010120000AM" class="tt" onmouseout="HideToolTip(ctl00_cphWorkforceScheduler_spnWorkforceToolTip_ctl00_cphWorkforceScheduler_thDay_10012010120000AM, ctl00_cphWorkforceScheduler_spnWorkforceToolTipMiddle_ctl00_cphWorkforceScheduler_thDay_10012010120000AM);" onmouseover="ShowToolTip(ctl00_cphWorkforceScheduler_spnWorkforceToolTip_ctl00_cphWorkforceScheduler_thDay_10012010120000AM, ctl00_cphWorkforceScheduler_spnWorkforceToolTipMiddle_ctl00_cphWorkforceScheduler_thDay_10012010120000AM, '135px');" style="background-repeat: repeat-x; text-align: center; font-size: 8pt; font-weight: normal; background-image: url(../Images/TitleGradientMiddle.JPG); height: 20px; width: 35px; text-decoration: none;">
</th>
<th id="ctl00_cphWorkforceScheduler_thDay_10022010120000AM" class="tt" onmouseout="HideToolTip(ctl00_cphWorkforceScheduler_spnWorkforceToolTip_ctl00_cphWorkforceScheduler_thDay_10022010120000AM, ctl00_cphWorkforceScheduler_spnWorkforceToolTipMiddle_ctl00_cphWorkforceScheduler_thDay_10022010120000AM);" onmouseover="ShowToolTip(ctl00_cphWorkforceScheduler_spnWorkforceToolTip_ctl00_cphWorkforceScheduler_thDay_10022010120000AM, ctl00_cphWorkforceScheduler_spnWorkforceToolTipMiddle_ctl00_cphWorkforceScheduler_thDay_10022010120000AM, '135px');" style="background-repeat: repeat-x; text-align: center; font-size: 8pt; font-weight: normal; background-image: url(../Images/TitleGradientMiddle.JPG); height: 20px; width: 35px; text-decoration: none;">

Etc, etc. I didn't paste everything since there's a lot of tags. Please assume that HTML is well formed.

A: 

Doesn't look like it's supported:

http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmltablerow.innertext%28v=VS.71%29.aspx

Have you tried reading each of the row's cells?

something like this maybe?

string rowData;
foreach(HTMLTableCell c in tblGridHeader.Rows[0].Cells)
{
   rowData += c.InnerHtml; //or c.InnerText
}
Abe Miessler
I tried using the cell property, but I'm having an issue with the rows saying there's no cells.
Idealflip
Do you not have any `TD` in your `TR`?
Abe Miessler
I have <th> in my <tr>
Idealflip
Can you post your HTML? According to MSDN `Use the HtmlTableCell class to programmatically control the <td> and <th> HTML elements in an HtmlTableRow object.` Link: http://msdn.microsoft.com/en-US/library/system.web.ui.htmlcontrols.htmltablecell%28v=VS.80%29.aspx
Abe Miessler
@Abe: I just updated my question with the code you asked for.
Idealflip
+1  A: 

I'm not a web guy, but a quick trip to MSDN revealed the HTMLTableRow.Cells property.

Ed Swangren
No, I think Ed is right. tblGridHeader.Rows[0] returns the first row in the table, hence the "HTMLTableRow" message
mjd79
Sorry, I forgot to mention I tried the Cells property as well. But it's saying there's no Cells.
Idealflip
Then... there are no cells.
Ed Swangren
+1  A: 

HTMLTableRow controls can't contain text, they can only contain other controls. What are you trying to do exactly? Chances are you should be looking at the Controls or Cells collection of HTMLTableRow, and then inspect the text (or further child controls) of its children.

jamietre
Hi, I've tried using cells as well.tables[j].AddCell(tblGridHeader.Rows[0].Cells[0].InnerText.ToString());But it says the Cell is empty, which it's not.<table><tbody><tr><th>i want this text</th></tr></tbody></table>
Idealflip
Since you said it works for a static table, it sounds like you are trying to read the contents of the cell before they exist. Have you actually added the cell to its owner row, and added its contents at the time you're doing this?
jamietre
A: 

Try something like

tblGridHeader.Rows[0].Cells[0].InnerText

Assuming you want the textual content from the row's first cell.

Jacob
That doesn't work for me since the rows are saying there's no cells...
Idealflip
A: 

Try tblGridHeader.Rows[0].Cells[0].InnerText;

drooksy
Doesn't work since rows is saying there's no cells present.
Idealflip
+2  A: 

If you have runat="server" to the TD,you can read it in the codebehind

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="MyProject.UI" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
 <title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
  <table>
    <tr>
      <td id="tdHere" runat="server">This is my content</td>
    </tr>
    <tr>
      <td>
          <asp:Button ID="btnPost" runat="server" Text="Post" onclick="btnPost_Click" />
          <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
      </td>
    </tr>
  </table>
</div>
</form>
</body>  
</html>

and im codebehind

using System;

namespace MyProject.UI
{
  public partial class test : System.Web.UI.Page
  {
     protected void Page_Load(object sender, EventArgs e)
     {
     }

     protected void btnPost_Click(object sender, EventArgs e)
     {
        lblMessage.Text = tdHere.InnerHtml;
     }
  }
}

This is from a working example which i tested

Shyju
I put the runat="server" on the <tr> tags, but the tr's are saying there's no cells or controls...
Idealflip
@Idealflip: I updated my answer with a code which works in my machine.Try that
Shyju
@Shyju: <th> are dynamically added to my <tr>'s. I need the text in my <th> tag. I believe the fact that my <tr> is saying there's no cells present, is the issue. Do I have to save a table in viewstate or something?
Idealflip