【发布时间】:2015-08-27 15:25:48
【问题描述】:
我正在尝试将一些 JQuery 添加到现有的 ASP.NET 应用程序中。在通常隐藏的 div 中包含一个 WebDataGrid,还有一个显示网格的按钮。
网格可能有也可能没有任何数据。我希望能够根据网格是否包含任何数据来更改显示网格的按钮的样式。问题是 ig_controls 似乎不包含 document.ready() 处理程序中的 WebDataGrid 对象。
这里是相关的 HTML -
<div id="grids">
<ig:WebDataGrid ID="WebDataGrid1" ... />
<button type="button" class="btnClose">Close</button>
</div>
.
.
.
<button type="button" id="btnShow">Show</button>
还有 javascript -
$(function() {
function UpdateShowButton() {
// When called from the click handler, 'grid'is non-null,
// otherwise, 'grid' is null.
var grid = ig_controls.WebDataGrid1;
if (grid.get_rows().get_length() > 0) {
$('#btnShow').addStyle('hasItems');
} else {
$('#btnShow').removeStyle('hasItems');
}
}
// View the grid when clicked.
$('#btnShow').on('click', function() {
$('#grids').show();
}
// Hide the grid, and update the "Show" button
$('#btnClose').on('click', function() {
// Hide the grid
$('#grids').hide();
UpdateShowButton(); // This call works
}
// Finally, on postbacks, ensure the "Show" button is styled properly
UpdateShowButton(); // This fails
});
直接用
获取网格var grid = $('#WebDataGrid')
在这两种情况下都是非空的,但没有给我 WebDataGrid 对象,而且我不确定如何从原始 HTML 中获取行数。
任何帮助将不胜感激。谢谢。
更新: 感谢 Diego,这个问题可以得到解决。而不是这个 -
// Finally, on postbacks, ensure the "Show" button is styled properly
UpdateShowButton(); // This fails
我试过了 -
// Finally, on postbacks, ensure the "Show" button is styled properly
setTimeout(UpdateShowButton, 1);
是的,这是 1. 将 UpdateButton 调用延迟一毫秒会导致 ig_controls 中可以访问 WebDataGrid 对象。
由于延迟持续时间微不足道(并且该应用仅供内部使用)我不介意保留此代码。但我仍然想找出为什么需要解决方法,或者找到一个修复'看起来不像这样的黑客。
【问题讨论】:
-
您是如何以及在何处包含已发布的脚本?
-
我在 .aspx 页面和外部 .js 文件中都试过了。在这两种情况下我都会得到相同的行为。
-
试过将它包含在
body的底部吗? -
这就是现在的位置,在
标签: jquery asp.net infragistics webdatagrid