繁体   English   中英

在后面的代码中访问动态生成的表行

[英]Accessing Dynamically generated table row in code behind

我在aspx页面中有带标题的表格。 我已经使用JavaScript将行动态添加到表中。 我需要访问服务器端新添加的表行。 我只能访问标题行,它alwasys show table仅具有一行,而不能访问新添加的行。

ASPX页面

<table runat="server" id="tblNew" clientidmode="Static">
                                        <tr>
                                            <th>
                                                Column1
                                            </th>
                                            <th>
                                                Column2
                                            </th>
                                        </tr>
                                    </table>

用于添加行的Javascript函数。

    var table = document.getElementById("SelectedItems");
    var newrow = table.insertRow(0);
    var newcel0 = newrow.insertCell(0);
    var newcel1 = newrow.insertCell(1);

CS TO访问表行

    int rowCount=tblNew.Rows.Count;

我总是把计数值当作一。 帮助表示赞赏。

您的JavaScript正在浏览器中执行。 您的CS代码正在服务器中执行。 在将结果html页面传输到Web浏览器之前,将执行服务器代码。

使用CS代码(而不是JavaScript代码)将行添加到表中。 这样可以解决问题。

我建议使用隐藏字段来管理计数:

<input type="hidden" value="0" runat="server" id="hiddenRowCount"/>

现在,每当您使用javascript将新行添加到表中时,都按如下所示增加计数器:

var table = document.getElementById("<%=tblNew.ClientID%>");
var newrow = table.insertRow(0);
newrow.insertCell(0);
newrow.insertCell(1);

// get current value of the hidden field
var rowCount = document.getElementById('<%=hiddenRowCount.ClientID%>').value;

if (rowCount != '') {
    rowCount = parseInt(rowCount) + 1;
} else {
    rowCount = 1;
}

document.getElementById('<%=hiddenRowCount.ClientID%>').value = rowCount;

现在,您可以在下面提到的asa后面的代码中获取总行数:

int totalRows = this.tblNew.Rows.Count + Convert.ToInt32(this.hiddenRowCount.Value);

注意:如果必须使用javascript动态删除原始文件,则还必须管理该计数器。

暂无
暂无

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

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