Tutorial :Why doesnt the AsyncCallback update my gridview?



Question:

I started working with delegates last week and i am trying to update my gridview async on the background. All goes well, no errors or such but i dont get a result after my EndInvoke. does anyone know what i am doing wrong?

Here is a code snippet:

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


Solution:1

I just ran a test using the same async calls in the order you call them. It runs fine here. I'd suspect you have a problem getting a handle to the Label control. Try breaking that line into a couple of lines to make sure you get the handle properly. Does Rows actually return a row? Does FindControl return the control you want? You should probably check that in both of your functions.

And as a side note, you might want to consider only indexing into Rows and using FindControl once. You would need to replace the object you pass into the IAsyncResult with one that can save a handle to a Label. Then you could do it once and assign it, then use in in UpdateWebserviceStatus.

Edit: Try this code.

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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »