views:

66

answers:

1

Hi all,

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

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);
    }
gbogumil
Thanks for your quick answer.. i went on and did some few things myself and after reading your answer it became clear that Rows doesnt return anything. I will try your suggestion now with updating the object to save the label handleEdit: I also updated the code above to the version i have at the moment.
Naruji
I just dont have a clue what to do with the Rows[...], maybe i should add a int and ++ everytime it comes across.. but thats just plain ugly if you ask me.
Naruji