Solved

Problem with UpdatePanel.Update method

Posted on 2011-09-25
2
683 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to implement server side field validation and display customized error messages to the client.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 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