繁体   English   中英

在document.ready中访问Infragistics WebDataGrid

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM