Click here to Skip to main content
15,905,073 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to do an asyncallback on page load of an aspx page.

The value returned from the callback of EndInvoke is assigned to a label field. But the problem is I'm not getting the value when running my application.

I've checked this with a break point too. I am unable to determine the cause of the problem.

I've tried with an AJAX updatepanel for the label - still not working.

Here is the code:

ASPX
XML
<asp:UpdatePanel ID="updLbl" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                You are in
                <asp:Label ID="lblPos" runat="server"></asp:Label><sup>th</sup> Position
            </ContentTemplate>
        </asp:UpdatePanel>


C#
public partial class _Default : System.Web.UI.Page 
{
    int pos;
    public delegate int  dlgtcaller(out int pos);
    protected void Page_Load(object sender, EventArgs e)
    {
        Async objCls = new Async();
        dlgtcaller call = new dlgtcaller(objCls.AsyncMethod);
        IAsyncResult result = call.BeginInvoke(out pos, new AsyncCallback(cbMethod), null);
    }
    public void cbMethod(System.IAsyncResult ar)
    {
        AsyncResult result = (AsyncResult)ar;
        dlgtcaller caller = (dlgtcaller)result.AsyncDelegate;
        int returnValue = caller.EndInvoke(out pos, result);
        lblPos.Text = pos.ToString();
    }
}

public class Async
{
     public int AsyncMethod(out int pos)
    {
        string SQL = "SELECT count(id) FROM test";
        string ConnectionString = "server=DEV-PC4\\SQLEXPRESS;Initial Catalog=Pradeep;User ID=******;Password=******;";
        SqlConnection con = new SqlConnection(ConnectionString);
        SqlCommand cmd = new SqlCommand(SQL, con);
        con.Open();
        pos = Convert.ToInt32(cmd.ExecuteScalar());
        return pos;
    }
}




But all i need is to put the value in lblPos.text that should be returned by the callback method. Is there any way..:confused:



Thanks in advance..
Posted
Updated 1-Apr-10 0:33am
v5

Why don't you use a class data member in the code-behind, set the value of that data member and use if in your HTML instead of setting the label's text property.

In the code-behind:

public class myclass
{
    public string mylabel = "";
}


In the HTML:

<ContentTemplate>
    You are in
    <asp:Label ID="lblPos" runat="server"><%=mylabel%></asp:Label><sup>th</sup> Position
</ContentTemplate>
 
Share this answer
 
Why are you trying to run the SQL asynchronously?

This basically won't work.

Nick
 
Share this answer
 
Thankyou Nick..

But all i need is to put the value in lblPos.text that should be returned by the callback method. Is there any way..:confused:
 
Share this answer
 
Sorry, but you've got this completely backwards.

In asp.net, a request must come from the client which is then handled by the server and the result is returned.

If you use BeginInvoke on the server, the request will most probably already have been completed by the time the callback method is called. That is why setting lblPos.Text doesn't do anything. The result has already been sent to the client.

An UpdatePanel, on the other hand, is a way to update a partial area of your page. It too works by sending a request to the server and handling the result that is sent back.

If you edit your question and say exactly what you're trying to achieve, we may be able to offer an alternative solution.

Nick
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900