将行id传递给循环C#

时间:2015-09-25 14:51:36

标签: c# html

我有一个表,其中每个td的id都是“A0XJ”形式,其中x和j的值表示行号和列号。我想以编程方式更新表数据内部文本的值(我将从数据表中获取的值。

<table border=1>
  <tr>
  <td id="A011"></td>
  <td id="A012"/><td>
  <td id="A013"></td>
  <td id="A014"></td>
  <td id="A015"></td>
    </tr>
  <tr>
  <td id="A021"></td>
  <td id="A022"></td>
  <td id="A023"></td>
  <td id="A024"></td>
  <td id="A025"></td>
    </tr>
  </table>

在c#代码中,我构建了一个循环,以便自动填充所有数据。我遇到的问题是我不确定我应该使用的数据类型。我正在使用字符串(绝对不是正确的类型)  我收到以下错误'字符串不包含InnerText的定义,并且没有扩展方法innertext:

     protected void FILL_DATA(object sender, EventArgs e)
{
    DataTable IACS = new DataTable();
        IACS= GenerateTransposedTableinCsharp(generateIacs());

        for (int i = 6; i <= 8; i++)
        {
            String  rowtext ="A0" + i;
            for (int j = 1; j <= 14; j++)
            {
                String text = rowtext + j;
                text.InnerText= IACS.Rows[i-5].Field<string>(j);
            }
        }

} 

2 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,则无法在表格中找到格式正确的 <!-- === === BODY COPY === === --> <h2>Watch now to investigate.</h2> <div id="popUpVideoBackground"></div> <div id="popUpVideoContainer"> <div id="videoCaption"></div> <h4 id="videoTime"></h4> <div><a href="javascript:closePopUpVideo()"><img id="closeIcon" src="images/favicons/close_icon.png" /></a> <video id="popUpVideo" width="640" height="480" controls="controls"><source src="" type="video/mp4" /> </video> </div> </div> <!-- === === Video 1 === === --> <div class="imgContainer"> <a class="ImageLink" href="javascript:playTrailer('http://cdn.sciex.com/BLT_Tease_pt1.mp4','Mission-X underway Part 1')"> <img class="overlayIcon" src="images/favicons/playBtn_40x36.jpg" /> <img class="videoImage" src="Images/campaigns/food/BLTteaser1_200x140.jpg" /> </a> </div> <div> <script type="text/javascript"> $(document).ready(function () { function playTrailer(video, title) { document.getElementById("videoCaption").innerHTML=title; document.getElementById("popUpVideoBackground").style.display="inline"; document.getElementById("popUpVideo").style.display="inline"; document.getElementById("closeIcon").style.display="inline"; document.getElementById("popUpVideoContainer").style.display="inline"; document.getElementById("popUpVideo").setAttribute('src',video); document.getElementById("popUpVideo").setAttribute('type',"video/mp4"); document.getElementById("popUpVideo").load(); document.getElementById("popUpVideo").play(); if(navigator.userAgent.indexOf("Chrome") != -1 ) { document.getElementById("popUpVideo").style.marginTop="0px"; document.getElementById("popUpVideo").style.height="460px"; document.getElementById("popUpVideoContainer").style.height="550px"; } if((navigator.userAgent.indexOf("MSIE") != -1 ) || (!!document.documentMode == true )) //IF IE > 10 { document.getElementById("popUpVideo").style.marginTop="100px"; document.getElementById("popUpVideo").style.height="480px"; document.getElementById("popUpVideoContainer").style.height="630px" ; } } function closePopUpVideo(){ document.getElementById("popUpVideoBackground").style.display="none"; document.getElementById("popUpVideo").style.display="none"; document.getElementById("popUpVideoContainer").style.display="none"; document.getElementById("closeIcon").style.display="none"; document.getElementById("popUpVideo").pause(); } 字符串&#34; A0XJ&#34;其中X是行,J是列。

然后你的问题是你没有正确构建你的字符串。通过内循环或外循环中的每次迭代,都没有正确更新text

以下是我的解决方案

text

答案 1 :(得分:1)

如果要将table元素视为可以从c#代码更新的控件,则需要添加runat="server"属性并为表提供ID:

<table border="1" id="tableIACS" runat="server">
    <tr>
        <td id="A011"></td>
        <td id="A012"></td>
        <td id="A013"></td>
        <td id="A014"></td>
        <td id="A015"></td>
    </tr>
    <tr>
        <td id="A021"></td>
        <td id="A022"></td>
        <td id="A023"></td>
        <td id="A024"></td>
        <td id="A025"></td>
    </tr>
</table>

然后,您可以使用FindControl()

通过ID引用其中的单元格
protected void FILL_DATA(object sender, EventArgs e)
{
    DataTable IACS = new DataTable();
    IACS = GenerateTransposedTableinCsharp(generateIacs());

    for (int i = 6; i <= 8; i++)
    {
        String rowtext = "A0" + i;
        for (int j = 1; j <= 14; j++)
        {
            String text = rowtext + j;
            HtmlTableCell cell = tableIACS.FindControl(text) as HtmlTableCell;
            if (cell != null)
                cell.InnerText = IACS.Rows[i - 5].Field<string>(j);

        }
    }
}