为什么异步回调不更新我的gridview?

为什么异步回调不更新我的gridview?,gridview,asynchronous,begininvoke,Gridview,Asynchronous,Begininvoke,我上周开始与代表一起工作,我正在尝试在后台更新我的gridview async。一切都进行得很顺利,没有错误之类的,但是我在EndInvoke之后没有得到结果。有人知道我做错了什么吗 以下是一段代码片段: public delegate string WebServiceDelegate(DataKey key); protected void btnCheckAll_Click(object sender, EventArgs e) { foreach

我上周开始与代表一起工作,我正在尝试在后台更新我的gridview async。一切都进行得很顺利,没有错误之类的,但是我在EndInvoke之后没有得到结果。有人知道我做错了什么吗

以下是一段代码片段:

    public delegate string WebServiceDelegate(DataKey key);

    protected void btnCheckAll_Click(object sender, EventArgs e)
    {
        foreach (DataKey key in gvTest.DataKeys)
        {
            WebServiceDelegate wsDelegate = new WebServiceDelegate(GetWebserviceStatus);
            wsDelegate.BeginInvoke(key, new AsyncCallback(UpdateWebserviceStatus), wsDelegate);
        }
    }

    public string GetWebserviceStatus(DataKey key)
    {
        return String.Format("Updated {0}", key.Value);
    }

    public void UpdateWebserviceStatus(IAsyncResult result)
    {
        WebServiceDelegate wsDelegate = (WebServiceDelegate)result.AsyncState;

        Label lblUpdate = (Label)gvTest.Rows[???].FindControl("lblUpdate");
        lblUpdate.Text = wsDelegate.EndInvoke(result);
    }

我只是按照调用顺序使用相同的异步调用运行了一个测试。这里运行良好。我怀疑你在获取标签控件的句柄时遇到了问题。试着把那条线分成几条线,以确保你正确地握住把手。行实际上返回一行吗?FindControl是否返回所需的控件?您可能应该在两个函数中检查一下

作为一个旁注,您可能需要考虑只对行进行索引,并使用FUNDCONCEL一次。您需要将传递到IAsyncResult的对象替换为可以将句柄保存到标签的对象。然后您可以执行一次并分配它,然后在UpdateWebserviceStatus中使用它

编辑:尝试此代码

        public delegate void WebServiceDelegate(DataKey key);

    protected void btnCheckAll_Click(object sender, EventArgs e)
    {
        foreach (DataKey key in gvTest.DataKeys)
        {
            WebServiceDelegate wsDelegate = new WebServiceDelegate(GetWebserviceStatus);
            wsDelegate.BeginInvoke(key, new AsyncCallback(UpdateWebserviceStatus), wsDelegate);
        }
    }

    public void GetWebserviceStatus(DataKey key)
    {
        DataRow row = gvTest.Rows[key.Value];
        System.Diagnostics.Trace.WriteLine("Row {0} null", row == null ? "is" : "isn't");

        Label lblUpdate = (Label)row.FindControl("lblUpdate");
        System.Diagnostics.Trace.WriteLine("Label {0} null", lblUpdate == null ? "is" : "isn't");

        lblUpdate.Text = string.Format("Updated {0}", key.Value);
    }

    public void UpdateWebserviceStatus(IAsyncResult result)
    {
    WebServiceDelegate wsDelegate = (WebServiceDelegate)result.AsyncState;
    DataKey key = wsDelegate.EndInvoke(result);
    }

谢谢你的快速回答。。我继续自己做了一些事情,在阅读了你的答案后,很明显,行不会返回任何结果。我现在将尝试你的建议,更新对象以保存标签句柄编辑:我还将上面的代码更新到目前的版本。我只是不知道如何处理行[…],也许每次遇到时我都应该添加int和++。。但如果你问我的话,那简直太难看了。