• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 766
  • Last Modified:

Problem with UpdatePanel.Update method

I have a simple ASP.NET web application. When the default page loads, I have a code behind that fires off an asynchronous method to get text that is being broadcast by a network device. When the asyncronous method calls back, I want to update a text box on an ASP.NET page, and update the UpdatePanel control that contains the text box. The problem is, when the callback returns and calls the Update method of the UpdatePanel control, the text box is not updating on my page. On the UpdatePanel I have set the UpdateMode=conditional, ChildrenAsTriggers=false, and have no explicit triggers defined on the form. I have included the code from the page and code behind below. You will notice I am popping up a message box at the end that contains the text that is supposed to be in the text box. It always shows the text I expect from the network device, but the textbox on the ASP form never updates.

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False">
        <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server" Height="123px" MaxLength="1000" 
                Rows="3" Width="411px"></asp:TextBox>
        </ContentTemplate>
    </asp:UpdatePanel>
    </asp:Content>

Open in new window

public partial class _Default : System.Web.UI.Page
    {
        private delegate string AsyncGCBeaconListener();

        public string beaconMessage { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            //this.beaconMessage = "Getting Global Cache beacon message. Please wait...";
            //this.beaconMessageTextBox.DataBind();
            ScriptManager1.RegisterAsyncPostBackControl(TextBox1);
            GlobalCacheBeaconListener bl = new GlobalCacheBeaconListener();
            AsyncGCBeaconListener abl = new AsyncGCBeaconListener(bl.GetBeaconMessage);
            abl.BeginInvoke(new AsyncCallback(GCBeaconListenerCallback), null);
        }

        private void GCBeaconListenerCallback(IAsyncResult ar)
        {
            AsyncResult result = (AsyncResult)ar;
            AsyncGCBeaconListener abl = (AsyncGCBeaconListener)result.AsyncDelegate;
            //this.beaconMessage = abl.EndInvoke(ar);
            this.TextBox1.Text = abl.EndInvoke(ar);
            this.UpdatePanel1.Update();
            MessageBox.Show(this.TextBox1.Text);
        }
    }

Open in new window

0
MonMuscJW
Asked:
MonMuscJW
1 Solution
 
SunnyDarkCommented:
It looks that by the time you reach the GCBeaconListenerCallback function the page was already completely loaded and sent back to the browser so any update to the panel will NOT be reflected in the html since that html is already sent to the browser...

The whole idea of the update panel is to update content AFTER the page completely loaded without refreshing the whole page, and usually the update is initiated by some action from the user or a client side script. That is what AJAX is all about...

You should either make your page synchronous (that means not using the whole async callback stuff) or cache the callback result on the server side and regularly check for update (at specified intervals) from the client side by some script. The most important thing to remember is that the server can never issue a request to the clients browser, only a response to a previous request.  
0
 
MonMuscJWAuthor Commented:
Thanks for the comments. I think I did not have a good complete understanding of update panel and script manager, and have since done a lot of research on the subject. I was thinking that changes to the client end could be initiated from the server as if there was an always an open connection like a socket between server and client. I realize now that that is not the way AJAX works.

Your thought of caching the result on the server and having the client side check for the result is a good one. I'll give that a shot.

I hope HTML5 starts to really take off...I think websockets would give me an even better way to do something like this.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now