Solved

Problem with UpdatePanel.Update method

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

13 Experts available now in Live!

Get 1:1 Help Now