Solved

Problem with UpdatePanel.Update method

Posted on 2011-09-25
2
667 Views
Last Modified: 2012-05-12
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
Comment
Question by:MonMuscJW
2 Comments
 
LVL 8

Accepted Solution

by:
SunnyDark earned 500 total points
ID: 36896440
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
 

Author Closing Comment

by:MonMuscJW
ID: 36896862
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question