既存の ASP.NET アプリに JQuery を追加しようとしています。通常非表示の 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')
両方のインスタンスで null ではありませんが、WebDataGrid オブジェクトが得られず、生の HTML から行数を取得する方法がわかりません。
どんな助けでも大歓迎です。ありがとう。
更新: ディエゴのおかげで、問題を回避できます。これの代わりに -
// 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 への呼び出しを 1 ミリ秒遅らせると、ig_controls で WebDataGrid オブジェクトにアクセスできるようになります。
遅延時間は重要ではない (そしてアプリは内部使用のみである) ため、このコードをそのままにしておいてもかまいません。そのようなハックのように。