views:

462

answers:

3

First of all, I'm new to Ajax and I don't quite understand everything about how it's work in Asp.Net.

I'm using Asp.Net 3.5 and I have a c# server code that run and when it's have finish it work, it's call a subscribed event that will write the result in a txtbox control.

c# code :

public partial class TestDBLoader : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        dbManager1.LoadDBCompleted += new DBManager.AsyncLoadDBDelegate(dbManager1_LoadDBCompleted);
        dbManager1.LoadDBAsync(sender, e, null);
    }

    public void dbManager1_LoadDBCompleted(object sender, EventArgs e)
    {
        txtResult.Text = "Finish!";
        updatePanel.Update();
    }
}

public partial class DBManager : System.Web.UI.UserControl
{
    public AsyncLoadDBDelegate asyncLoadDB;
    public delegate void AsyncLoadDBDelegate(object sender, EventArgs e);
    public event AsyncLoadDBDelegate LoadDBCompleted;

    private void StartLoad(object sender, EventArgs e)
    {
        // Not the true code, only an example ...
        for (int i = 0; i <= 10; i++)
        {
            Thread.Sleep(1000);
        }

        LoadDBCompleted(sender, e);
    }

    public IAsyncResult LoadDBAsync(object sender, EventArgs e, AsyncCallback callback)
    {
        IAsyncResult asyncResult;

        asyncLoadDB = new AsyncLoadDBDelegate(StartLoad);

        asyncResult = asyncLoadDB.BeginInvoke(sender, e, callback, null);

        return asyncResult;
    }
}

Asp code :

<asp:ScriptManager ID="ScriptManager" runat="server" />   
<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="dbManager1" EventName="LoadDBCompleted" />
    </Triggers>
    <ContentTemplate>
        <uc:DBManager ID="dbManager1" runat="server" />
        <asp:TextBox ID="txtResult" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>

What am I doing wrong ? If I go in debug, i notice that my method dbManager1_LoadDBCompleted is call but it's doesn't update the textbox...

EDIT : I update the code to be more realistic and understandable.

EDIT2 : If there is a way of doing it without using the UpdatePanel, please let me know how.

+1  A: 

the id of your textbox is txtbox1 but you are setting txtbox.text ...

I correct it in my post, it was a mistake when I copy/paste it
Melursus
A: 

Please keep in mind that this is technically not "AJAX". An updatepanel is just basically saying "send me the HTML that goes in this spot after a postback", which can be pretty chatty.

Also, you probably need to share more code to help diagnose this. What is firing WorkFinished?

Nicholas H
I edit my message with more code
Melursus
A: 

It's most likely that the UpdatePanel's Update method isn't being fired, which is why there is no change to its contents.

You need to either ensure the control which causes WorkFinished to execute is within the UpdatePanel's ContentTemplate, add the ID of the control calling WorkFinished to the Triggers collection as an AsynchronousPostbackTrigger or add UpdatePanel.Update(); to the end of WorkFinished's body, forcing the UpdatePanel to reload itself.

Be careful with UpdatePanels though, they can be very performance-intensive due to their nature, something I covered here - http://www.aaron-powell.com/blog/august-2008/optimising-updatepanels.aspx

Slace
I edit my message with your recommendation, but it's still doesn't work
Melursus