asp:DataGrid可见性与异步? [英] asp:DataGrid visibility with async?

查看:96
本文介绍了asp:DataGrid可见性与异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 4.5.2,Windows 8.1 x64上的IIS 8.5。我在/ localhost /有一个ASP.NET网站。

.NET 4.5.2, IIS 8.5 on Windows 8.1 x64. I have a single ASP.NET web site at /localhost/.

这里我有一个真正的异步难题,我已经苦苦挣扎了两天。

I've got a real async-puzzle here that I've been wrestling with for two days now.

我正在重新整理一些长时间运行的报告以使其异步,但是我可以用非常简单的代码来演示我的问题。

I'm reworking some long-running reports to be async, but I can demonstrate my problem with very simple code.

我的页面上有一个asp:DataGrid我最初设置了visible = false。仅在填充时才可见。我的问题是,如果填充的代码是异步的,我看不到网格!

I have an asp:DataGrid on my page that I've initially set visible="false". It should only be visible if it is populated. My problem is, if the code that populates it is async I don't see the grid!

这里是标记:

<body>
    <form runat="server">
        <asp:datagrid ID="grid" visible="false" Runat="server"></asp:datagrid>
    </form>
</body>

在后面的代码中,此代码工作:

In the code-behind, this code WORKS:

void Page_Load()
{
    grid.DataSource = new Dictionary<string, string>()
    {
        { "col1", "value1" }
    };
    grid.DataBind();
    grid.Visible = true;
}

现在,如果我进行两项更改:

Now if I make two changes:

将Async = True添加到@Page指令
用此替换Page_Load

Add Async="True" to the @Page directive Replace Page_Load with this

void Page_Load()
{
    this.RegisterAsyncTask(new PageAsyncTask(async () =>
        {
            grid.DataSource = new Dictionary<string, string>()
            {
                { "col1", "value1" }
            };
            grid.DataBind();
            grid.Visible = true;
        }));
}

不渲染网格。请注意,即使委托中没有等待,我也使用了async关键字。我已经尝试了几种方法,所有方法都得到了相同的结果:

The grid is not rendered. Note that I've used the async keyword even though there is no await in the delegate. I have tried several things, all with the same result:


  1. 添加一个等待任务Task.Delay(...)

  2. 删除async关键字并返回Task.FromResult(0)

我已覆盖并检测了页面生命周期事件查看何时填充网格以及网格的可见性如何变化。这是我看到的输出:

I have overridden and instrumented the page lifecycle events to see when the grid is populated and how its visiblity changes. This is the output I see:

OnPreInit: Visible=False Rows=0 Thread=63
OnInit: Visible=False Rows=0 Thread=63
OnInitComplete: Visible=False Rows=0 Thread=63
OnPreLoad: Visible=False Rows=0 Thread=63
OnLoad: Visible=False Rows=0 Thread=63
OnLoadComplete: Visible=False Rows=0 Thread=63
OnPreRender: Visible=False Rows=0 Thread=63
Async 1: Visible=False Rows=0 Thread=63
Async 2: Visible=True Rows=1 Thread=63
OnPreRenderComplete: Visible=True Rows=1 Thread=63
OnSaveStateComplete: Visible=True Rows=1 Thread=63
Render 1: Visible=True Rows=1 Thread=63
Render 2: Visible=True Rows=1 Thread=63

异步1和 2位于委托内部网格的两侧。您可以看到网格正在排成一行并且其可见性为true,但尚未呈现。如果我用第一个代码示例同步填充网格,那么一切都很好。

"Async 1" and "2" are on either side of the grid population inside the delegate. You can see the grid is getting a row and its visibility gets true, yet it isn't rendered. If I populate the grid with my first code sample, synchronously, all is well.

另一注:我可以将网格封装在另一个控件中以使其正常工作:

Another note: I can enclose the grid in another control to make it work:

<body>
    <form runat="server">
        <div id="container" visible="false" runat="server">
            <asp:datagrid ID="grid" Runat="server"></asp:datagrid>
        </div>
    </form>
</body>

似乎是asp:datagrid上的visible = false本身将其搞砸了。

It seems to be the visible="false" on the asp:datagrid itself that screws this up.

我在做什么错了?

推荐答案

尝试放置网格在UpdatePanel中,并在UpdatePanel的OnLoad事件上调用async方法(以将数据加载到Grid中并管理其可见性)

Try placing the Grid in an UpdatePanel and call the async method (to load the data in the Grid and manage its visibility) on the OnLoad event of the UpdatePanel

这篇关于asp:DataGrid可见性与异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆