[英]Accessing Infragistics WebDataGrid in document.ready
我試圖將一些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')
在兩個實例中均為非null,但沒有給我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的調用延遲一毫秒會導致WebDataGrid對象在ig_controls中可訪問。
由於延遲時間微不足道(並且該應用僅供內部使用),我不介意保留此代碼。但是我仍然想找出為什么需要解決方法,或者找到一個看起來不可行的解決方案像這樣的黑客。
最可能的情況是,在某些時候,Infragistics網格的初始化使用了一些異步功能。 通過使用毫秒級的延遲,您可以使代碼在Infragistic之后執行。 實際的毫秒數無關緊要。
如果您想獲得更簡潔的代碼,我會問Infragistics,該怎么辦或正在發生什么。
祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.