我已经完成了一些搜索,尝试了几个来自不同帖子的解决方案,但似乎无法让它们工作。基本思想是...我正在自定义一个现有的用户控件,它将动态生成的数据放到单个行的列表中。然后它有一个“编辑”链接按钮,它执行回发并用可编辑字段重建表格。我发现我正在使用一些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