Solved

Problem with UpdatePanel.Update method

Posted on 2011-09-25
2
662 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now