我已經完成了一些搜索,嘗試了幾個來自不同帖子的解決方案,但似乎無法讓它們工作。基本思想是...我正在自定義一個現有的用戶控件,它將動態生成的數據放到單個行的列表中。然後它有一個「編輯」鏈接按鈕,它執行回發並用可編輯字段重建表格。我發現我正在使用一些jQuery將錶轉換成2行表,分成多列(更容易,然後試圖重新設計在C#中的數據創建/標記)。第一次加載頁面時,它完美地工作。然而,當我點擊「編輯」鏈接按鈕,它適當地做回發,但jQuery不會觸發。我嘗試了幾種配置無濟於事。這裏是jQuery的:無法讓jQuery在回發時觸發
private void RegisterScripts()
{
StringBuilder sbScript = new StringBuilder();
sbScript.Append("\tvar tables = $('table[id*=\"tblAttribute\"]');\n");
sbScript.Append("\tfor (var i = 0; i < tables.length; i++) {\n");
sbScript.Append("\t\tvar newTable = document.createElement('table');\n");
sbScript.Append("\t\tvar columns = 2;\n");
sbScript.Append("\t\tfor(var c = 0; c < columns; c++) {\n");
sbScript.Append("\t\t\tnewTable.insertRow(c);\n");
sbScript.Append("\t\t\tfor(var r = 1; r < tables[i].rows.length ; r++) {\n");
sbScript.Append("\t\t\t\tnewTable.rows[c].insertCell(r-1);\n");
sbScript.Append("\t\t\t\tnewTable.rows[c].cells[r-1].innerHTML = tables[i].rows[r].cells[c].innerHTML;\n");
sbScript.Append("\t\t\t\tnewTable.rows[c].cells[r-1].className = tables[i].rows[r].cells[c].className;\n");
sbScript.Append("\t\t\t\ttables[i].rows[r].style.display = 'none';\n");
sbScript.Append("\t\t\t}\n");
sbScript.Append("\t\t}\n");
sbScript.Append("\t\tnewTable.className = tables[i].className;\n");
sbScript.Append("\t\ttables[i].parentNode.appendChild(newTable);\n");
sbScript.Append("\t}\n");
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "RowsToColumnsScript", sbScript.ToString(), true);
}
這裏是在Page_Load週期內的呼叫:
protected void Page_Load(object sender, EventArgs e)
{
RegisterScripts();
// Other Stuff //
}
我也曾嘗試RegisterClientScriptBlock()
與RegisterStartupScript()
代替,得到了相同的結果。我錯過了什麼?
編輯2: 這是腳本,因爲它被添加到客戶端頁面。我複製右出頁面的源代碼(減去我的縮寫):
<script type="text/javascript">
//<![CDATA[
var tables = $('table[id*="tblAttribute"]');
for (var i = 0; i < tables.length; i++) {
var newTable = document.createElement('table');
var columns = 2;
for(var c = 0; c < columns; c++) {
newTable.insertRow(c);
for(var r = 1; r < tables[i].rows.length ; r++) {
newTable.rows[c].insertCell(r-1);
newTable.rows[c].cells[r-1].innerHTML = tables[i].rows[r].cells[c].innerHTML;
newTable.rows[c].cells[r-1].className = tables[i].rows[r].cells[c].className;
tables[i].rows[r].style.display = 'none';
}
}
newTable.className = tables[i].className;
tables[i].parentNode.appendChild(newTable);
}
// Other js registered from other usercontrols
</script>
您使用的是UpdatePanel嗎? –
您可以放置警告或調試器指令來查看代碼是否被執行。 –
@KarlAnderson - 不,我目前沒有使用更新面板。 – gmaness