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