简体   繁体   English

将表格从C#代码传递到javascript函数

[英]Passing tables from C# code behind to a javascript function

I have a user control that is part of an update panel. 我有一个用户控件,它是更新面板的一部分。 The user control is a heading( tag) and a asp Table. 用户控件是一个heading(标签)和一个asp表。 The asp:Table is defined in the ascx file only with the headers. 在ascx文件中仅使用标题定义了asp:Table。 The contents of this table are updated dynamically from the code behind by reading a csv file. 通过读取csv文件,此表的内容从后面的代码动态更新。 This set up is within an Update panel which updates every minute. 此设置位于“更新”面板中,该面板每分钟更新一次。 After every minute the csv file gets updated and hence the table needs to be updated. 每分钟之后,csv文件将更新,因此需要更新表。

Here is the tricky part. 这是棘手的部分。 Before the table is I updated I need to save a copy of the old table and then update the new table. 在更新表之前,我需要保存旧表的副本,然后更新新表。 Once the new table is updated and the page is about to be loaded I need to call a javascript function from in the page_load handler and pass the two tables. 一旦新表被更新并且页面即将被加载,我需要在page_load处理程序中调用javascript函数并传递两个表。 Inside the javascript function I need to compare the old table and the new table cell by cell and do some work based on the result of the comparison. 在javascript函数内部,我需要逐个单元比较旧表和新表,并根据比较结果进行一些工作。

Here is how I copy the data from the table to another table before updating it. 这是我在更新数据之前将数据从表复制到另一个表的方法。

TableCell tableCell;
TableRow tableRow;
for (int i = 0; i < Table1.Rows.Count; i++)
{
    tableRow = new TableRow();
    for (int j = 0; j < Table1.Rows[i].Cells.Count; j++)
    {
        tableCell = new TableCell();
        tableCell.Text = Table1.Rows[i].Cells[j].Text;
        tableRow.Cells.Add(tableCell);
    }
    oldTable.Rows.Add(tableRow);
}

But for some reason when I pass the tables to the javascript function and access the cells in javascript I see only the headers and not any values in the old table. 但是由于某种原因,当我将表传递给javascript函数并访问javascript中的单元格时,我只看到标头,而看不到旧表中的任何值。 But when I access the cells in my code behind itself I can see the values. 但是,当我访问自己背后的代码中的单元格时,可以看到这些值。

My HTML is 我的HTML是

<table id="ContentPlaceHolderBody_ContentPlaceHolderBody_TradxPriceTable_5_oldTable" ClientID="oldTable">
    <tr>
        <td colspan="3">6M EURIBOR</td>
        <td colspan="3">6M EURIBOR</td>
    </tr><tr>
         <td>Instr</td>
         <td>Bid</td>
         <td>Ask</td>
         <td>Instr</td>
         <td>Bid</td>
         <td></td>
    </tr><tr>
         <td></td>
         <td></td>
         <td></td>
         <td colspan="3">BASIS 3s6s</td>
    </tr><tr>
          <td></td>
          <td></td>
          <td></td>
          <td>Instr</td>
          <td>Bid</td>
          <td>Ask</td>
    </tr><tr>
         <td></td>
         <td></td>
         <td></td>
         <td></td>
         <td></td>
         <td></td>
    </tr>
</table>

My javascript is 我的JavaScript是

function foo(table1,table2)
{
   var oldTable = document.getElementById(table1);
   var newTable = document.getElementById(table2);
   alert(oldTable.rows[2].cells[1].innerHTML+" "+newTable.rows[2].cells[1].innerHTML);
}

use html tables and form a string with the tables and the values and pass that string to javascript. 使用html表并与表和值组成一个字符串,然后将该字符串传递给javascript。

string newTable = "<Table><Tr><Td>"+ somevalue +"</Td></Tr></Table>"

in javascript 在JavaScript中

var newT = '<%= newTable %>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM