首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在WebDataGrid中访问非脆弱性document.ready

在WebDataGrid中访问非脆弱性document.ready
EN

Stack Overflow用户
提问于 2015-08-27 15:25:48
回答 1查看 601关注 0票数 0

我正在尝试将一些JQuery添加到现有的ASP.NET应用程序中。在通常隐藏的div中包含一个WebDataGrid,还有一个显示网格的按钮。

网格可能有也可能没有任何数据。我希望能够更改按钮的样式,它显示网格取决于网格是否包含任何数据。问题是,ig_controls似乎没有在document.ready()处理程序中包含WebDataGrid对象。

这是相关的HTML -

代码语言:javascript
复制
<div id="grids">
    <ig:WebDataGrid ID="WebDataGrid1" ... />
    <button type="button" class="btnClose">Close</button>
</div>
.
.
.
<button type="button" id="btnShow">Show</button>

还有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
});

直接获得网格

代码语言:javascript
复制
var grid = $('#WebDataGrid')

在这两个实例中都是非空的,但没有给出WebDataGrid对象,我也不知道如何从原始的HTML中获取行计数。

任何帮助都将不胜感激。谢谢。

更新:多亏了迭戈,这个问题可以解决。而不是这个-

代码语言:javascript
复制
// Finally, on postbacks, ensure the "Show" button is styled properly
UpdateShowButton(); // This fails

我试过这个-

代码语言:javascript
复制
// Finally, on postbacks, ensure the "Show" button is styled properly
setTimeout(UpdateShowButton, 1);

是的,这是一个1。将对UpdateButton的调用延迟一毫秒,结果是WebDataGrid对象在ig_controls中是可访问的。

由于延迟时间是微不足道的(而且应用程序仅供内部使用),所以我不介意把这段代码留在里面。但我还是想找出解决办法的必要性,或者找一个看起来不像黑客的解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-27 18:29:20

最有可能的情况是,在某个时候,不碎片网格的初始化使用了一些异步函数。通过使用毫秒的延迟,您的代码将在不碎片整理之后执行。实际的毫秒并不重要。

如果你想得到一个更干净的代码,我会问,不碎片整理,可以做什么,或正在发生什么。

祝好运!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32253491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档