简体   繁体   English

使用JavaScript runat =“ server”解析的数据向html表添加行

[英]Add rows to html table using data parsed by Javascript runat=“server”

I am having a little problem here. 我在这里有一个小问题。 I have a <script language ="JavaScript" runat="server"> code that reads a table from a website, parses data and stores it in a 2-D array. 我有一个<script language ="JavaScript" runat="server">代码,该代码从网站中读取表格,解析数据并将其存储在二维数组中。 It works correctly; 它可以正常工作; I can display the data using Response.Write. 我可以使用Response.Write显示数据。

Now, I would like to use the data in the array to construct my own table and display it in the browser. 现在,我想使用数组中的数据来构造自己的表并将其显示在浏览器中。 I thought I would just add: 我以为我会补充:

</head> 
<body onload="tableCreator();">
<table id="table" border="1">
    <tr> 
    <td id="Document Number">Document Number</td>
    <td id="Document Link">Document Link</td>
    <td id="Date Filed">Date Filed</td>
    <td id="Date Entered">Date Entered</td>
    <td id="Date Terminated">Date Terminated</td>
    <td id="Description">Description</td>
    </tr>
</table> 
<body> 
</html>

after script ends, but this is where problems start. 脚本结束后,但这是问题开始的地方。 I don't know where to insert the tableCreator() function. 我不知道在哪里插入tableCreator()函数。 If I do it inside the (script language ="JavaScript" runat="server") code (in order to be able to use the array's data), the function is not called. 如果我在(脚本语言=“ JavaScript” runat =“ server”)代码内执行此操作(以便能够使用数组的数据),则不会调用该函数。 When I change the tag to (script language ="JavaScript") (no runat="server"), the function is called, but the script isn't run (I have multiple Response.Write and nothing, but the table, is being printed) 当我将标签更改为(脚本语言=“ JavaScript”)(没有runat =“ server”)时,该函数被调用,但脚本未运行(我有多个Response.Write,但是表却没有正在打印)

function tableCreator () {
  var table = document.getElementById("table");
  var rowCount = table.rows.length;
  var row;
  row = table.insertRow(rowCount);
  var cell1 = row.insertCell(0);
  cell1.innerHTML = "1";
}

Any ideas? 有任何想法吗?

You are confusing server-side code and client-side code. 您正在混淆服务器端代码和客户端代码。

JavaScript can't access a 2d array that's created on the server (adding runat="server" to you script tag will not give you access to your server-side variables). JavaScript无法访问在服务器上创建的二维数组(将runat="server"添加到script标签将无法访问服务器端变量)。 You would need to serialize the array in your server-side language and then make it accessible to the client (a hidden field would work well for this). 您将需要使用服务器端语言序列化该数组,然后使其可供客户端访问(隐藏字段对此将非常有用)。

Alternatively, if you have the data server-side, why create the table client-side. 另外,如果您有数据服务器端,为什么还要创建表客户端。 Simply create the table server-side when the page loads. 在页面加载时,只需在服务器端创建表。

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

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