2010-04-06 27 views
1

我上週開始與代表一起工作,我試圖在後臺更新我的gridview異步。一切順利,沒有錯誤或類似的,但我沒有得到我的EndInvoke後的結果。有誰知道我做錯了什麼?爲什麼AsyncCallback不能更新我的GridView?

下面的代碼片段:

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

回答

0

我只是用你給他們打電話的順序相同的異步調用運行測試。它在這裏運行得很好。我懷疑你得到一個Label控件句柄的問題。嘗試將該行分成幾行,以確保正確得到句柄。行實際上是否返回一行? FindControl是否會返回你想要的控件?你可能應該檢查你的兩個函數。

作爲一個方面說明,您可能只想考慮索引到行並使用FindControl一次。您需要將您傳遞給IAsyncResult的對象替換爲可以將句柄保存到Label的對象。然後你可以做一次並分配它,然後在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); 
    } 
+0

感謝您的快速回答..我繼續做了一些自己的事情,並在閱讀您的答案後,它變得清晰,行不返回任何東西。我會嘗試更新對象以保存標籤句柄 編輯:我也更新了上面的代碼到我現在的版本。 – Naruji 2010-04-06 14:16:16

+0

我只是不知道如何處理行,也許我應該添加一個int和++每遇到它..但如果你問我,這只是簡單的醜陋。 – Naruji 2010-04-06 14:25:06