简体   繁体   English

在document.ready中访问Infragistics WebDataGrid

[英]Accessing Infragistics WebDataGrid in document.ready

I'm trying to add some JQuery to an existing ASP.NET app. 我试图将一些JQuery添加到现有的ASP.NET应用程序中。 There is a WebDataGrid contained within a div that is usually hidden, and a button which will display the grid. 在div中通常包含一个WebDataGrid,通常将其隐藏,并显示一个网格按钮。

The grid may or may not have any data. 网格可能有也可能没有任何数据。 I'd like to be able to change the style of the button which displays the grid depending on if the grid contains any data or not. 我希望能够更改显示网格的按钮的样式,具体取决于网格是否包含任何数据。 The problem is that ig_controls doesn't appear to contain the WebDataGrid object in the document.ready() handler. 问题在于ig_controls在document.ready()处理函数中似乎不包含WebDataGrid对象。

Here is the relevant HTML - 这是相关的HTML-

<div id="grids">
    <ig:WebDataGrid ID="WebDataGrid1" ... />
    <button type="button" class="btnClose">Close</button>
</div>
.
.
.
<button type="button" id="btnShow">Show</button>

And the javascript - 和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
});

Getting the grid directly with 直接使用

var grid = $('#WebDataGrid')

is non-null in both instances, but doesn't give me the WebDataGrid object, and I'm not sure how to get the row count from the raw HTML. 在两个实例中均为非null,但没有给我WebDataGrid对象,并且我不确定如何从原始HTML获取行数。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

UPDATE : Thanks to Diego, the issue can be worked around. 更新 :感谢Diego,可以解决此问题。 Instead of this - 代替这个-

// Finally, on postbacks, ensure the "Show" button is styled properly
UpdateShowButton(); // This fails

I tried this - 我试过了-

// Finally, on postbacks, ensure the "Show" button is styled properly
setTimeout(UpdateShowButton, 1);

Yes, that's a 1. Delaying the call to UpdateButton by a single millisecond results in the WebDataGrid object being accessible in ig_controls. 是的,这是1。将对UpdateButton的调用延迟一毫秒会导致WebDataGrid对象在ig_controls中可访问。

Since the delay duration is insignificant (and the app is for internal use only) I don't mind leaving this code in. But I'd still like to find out why the workaround is necessary, or find a fix that doesn't seem like such a hack. 由于延迟时间微不足道(并且该应用仅供内部使用),我不介意保留此代码。但是我仍然想找出为什么需要解决方法,或者找到一个看起来不可行的解决方案像这样的黑客。

The most probable scenario is that, at some point, Infragistics grid's initialization uses some async function. 最可能的情况是,在某些时候,Infragistics网格的初始化使用了一些异步功能。 By using a millisecond delay you are getting your code to be executed after Infragistic's. 通过使用毫秒级的延迟,您可以使代码在Infragistic之后执行。 The actual millisecond is not what matters. 实际的毫秒数无关紧要。

If you want to get a cleaner code I would ask Infragistics what can be done or what is happening. 如果您想获得更简洁的代码,我会问Infragistics,该怎么办或正在发生什么。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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