异步调用.NET方法和完成时绑定到网格 [英] Call .NET method asynchronously and bind to grid upon completion

查看:236
本文介绍了异步调用.NET方法和完成时绑定到网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Container.RetrieveItems()调用一个服务,这需要一段时间,所以我想异步调用它(之后被检索的项目,他们都设置为容器类的List属性)。在检索到的项目完成后,我想它更新的GridView这是一个UpdatePanel内(UpdatePanel的模式=条件和ScriptManager的的EnablePartialRendering =真。UpdatePanel中没有触发项目)。

Container.RetrieveItems() calls a service which takes a while so I would like to call it asynchronously (after the items are retrieved they are set to the List property of the Container class). Upon completion of retrieving the items, I would like it to update a gridView which is inside an updatePanel (updatePanel Mode="Conditional" and ScriptManager EnablePartialRendering="true". UpdatePanel has no trigger items).

我已经设置断点,并通过每一步台阶。被检索的项目,电网数据绑定然后调用更新。无异常被抛出但网格不与内容更新。如果我设置的UpdatePanel使用触发器和Timer.OnTick事件它的作品完美的更新,但是我只需要检索项目后,使射击手册UpdatePanel.Update()服务调用完成后,将是理想的它来更新。

I have set breakpoints and stepped through each step. The items are retrieved, grid is databound then it calls update. No exceptions are being thrown but the grid is not updating with the content. If I set the UpdatePanel to update using a trigger and Timer.OnTick event it works perfect, however I only need it to update after the items are retrieved so firing the manual UpdatePanel.Update() upon completion of the service call would be ideal.

我已经做了相当多的寻找,但所有的答案是:你忘了调用DataBind()

I have done quite a bit of searching, but all the answers are 'You forgot to call DataBind()'

有什么我失踪?

    private void UpdateGrid()
    {
        grid.DataSource = Container.List;
        grid.DataBind();
        updatePanel.Update();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        var task = Task.Factory.StartNew(Container.RetrieveItems);
        task.ContinueWith((x) => UpdateGrid());
    }

更新:
我成立了一个简单的测试,试图找出问题。我创建了一个标签的Text属性将一个方法完成时进行更新。当页面加载时,它调用的方法,当方法完成了它叫做updatePanel.Update(),但没有任何变化。

Update: I set up a simpler test to try to identify the issue. I created a label whose Text property would be updated upon completion of a method. When the page loaded, it called the method and when the method finished it called updatePanel.Update() but no change.

每Jaimes的意见,然后我试图调用Button_click的回传里面的手动更新,它的确更新的标签。所以这就是为什么我的当前设置不工作,虽然我还在寻找对异步任务完成更新内容的最好方式。

Per Jaimes' advice, I then tried calling the manual update inside the postback of a Button_click and it did indeed update the label. So that is why my current setup is not working, although I'm still searching for the best way to update content on completion of an asynchronous task.

推荐答案

杰米是在正确的轨道上。呈现页面时后服务器无法推送新的数据到客户端。客户端必须发起的请求。我会设置一个计时器或使用JavaScript 的setTimeout 客户端定期轮询服务器,看它是否完成。

Jamie is on the right track. Your server cannot "push" new data to the client after the page is rendered. The client has to initiate a request. I would set a timer or use a JavaScript setTimeout on the client to regularly poll the server to see if it completed.

另一种方法是设置Web服务的服务器上,然后从你的页面调用它。这将在一个新的线程执行异步只要结果是可更新的网格。

Another approach is to set up a web service on the server, then call it from your page. This will be executed in a new thread and update the grid asynchronously as soon as results are available.

首先,你需要建立一个WCF Web服务。有成千上万的这个文章,如果你不能确定。这里就是其中之一:的http://www.$c$cproject.com/Articles/16973/Simple-Web-Service-using-WCF-Windows-Communication

First you need to set up a WCF Web Service. There are thousands of articles about this if you aren't sure. Here is one of them: http://www.codeproject.com/Articles/16973/Simple-Web-Service-using-WCF-Windows-Communication

下面是我的一个项目的一些示例code:

Here is some sample code from one of my projects:

namespace UI.Web.WebServices
{
    [ServiceContract(Namespace = "WebServices")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class WebServiceName
    {
        [OperationContract]
        [WebInvoke(ResponseFormat = WebMessageFormat.Json)]
        public FacilityResult[] MethodName()
        {
            FacilityService facilityService = new 
            return facilityService .GetAssignments()).ToArray();
        }
    }
}

 

接下来,您将需要从网页数据绑定网格,使用JavaScript:的http://blog.ashmind.com/2007/06/21/client-side-databinding-with-aspnet-ajax-futures/

Next you will need to databind the grid from your page, using JavaScript: http://blog.ashmind.com/2007/06/21/client-side-databinding-with-aspnet-ajax-futures/

下面是从同一项目的一些示例code。该项目使用jQuery,而不是纯JavaScript像中的链接样本:

Here is some sample code from the same project. This project uses jQuery and not pure JavaScript like the samples in the links:

<script type="text/javascript">
    function getGridData() {
        var payload = { }; // Put data to include here, if any

        $.ajax({
            type: "POST",
            url: "/WebServiceName.svc/MethodName",
            data: JSON.stringify(payload),
            contentType: "application/json",
            dataType: "json",
            success: function (msg) {
                bindResultsToGrid(msg.d);
            },
            error: function (xhr) { alert('error' + xhr.responseText); }
        });
    }

function bindResultsToGrid(data)
{
    ...
}
</script>

参考
<一href=\"http://aspalliance.com/1301_Unveil_the_Data_Binding_Architecture_inside_Microsoft_ASPNET_Ajax_10__Part_1\" rel=\"nofollow\">http://aspalliance.com/1301_Unveil_the_Data_Binding_Architecture_inside_Microsoft_ASPNET_Ajax_10__Part_1

<一个href=\"http://aspalliance.com/1301_Unveil_the_Data_Binding_Architecture_inside_Microsoft_ASPNET_Ajax_10__Part_2\" rel=\"nofollow\">http://aspalliance.com/1301_Unveil_the_Data_Binding_Architecture_inside_Microsoft_ASPNET_Ajax_10__Part_2

这篇关于异步调用.NET方法和完成时绑定到网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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