首页 文章

使用c#从<th>获取文本

提问于
浏览
1

我需要从HTML表中提取一些文本

我试过用

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

但我得到了错误

“HTMLTableRow”不支持InnerText属性 .

我也试过InnerHTML,但仍然没有去 .

我确实尝试使用cells属性,但是我收到了错误

指定的参数超出了有效值的范围 .

Note: 我刚刚在静态表上尝试了cells属性,但它确实有效 . 所以我想这与我的表动态填充有关?

视觉工作室编辑的代码:

<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>

浏览器代码:

<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;">

等等,我没有粘贴所有内容,因为有很多标签 . 请假设HTML格式正确 .

6 回答

  • 1

    如果你有TD的runat =“server”,你可以在代码隐藏中读取它

    <%@ 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">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <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>
    

    和我的代码隐藏

    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;
         }
      }
    }
    

    这来自我测试的一个工作示例

  • 0

    我不是一个网络人,但快速访问MSDN揭示了HTMLTableRow.Cells属性 .

  • -1

    HTMLTableRow 控件不能包含文本,它们只能包含其他控件 . 你准备做什么?您可能应该查看 HTMLTableRowControlsCells 集合,然后检查其子项的文本(或其他子控件) .

  • 2

    尝试类似的东西

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

    假设您想要行的第一个单元格中的文本内容 .

  • 1

    尝试

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

    看起来不支持:

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

    你试过读过每一行的细胞吗?

    这样的事可能吗?

    string rowData;
    foreach(HTMLTableCell c in tblGridHeader.Rows[0].Cells)
    {
       rowData += c.InnerHtml; //or c.InnerText
    }
    

相关问题