New to Ajax and still getting the hang of how it works in Asp.Net.
Using Asp.Net 3.5 with a c# server code that runs and then calls a subscribed event to update text in a textbox 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)
{
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>
Debugging shows that dbManager1_LoadDBCompleted is being called but not updating the textbox. What could be the issue?
EDIT : Revised code for more clarity.
EDIT2 : Any alternative methods rather than using the UpdatePanel? Please advise.