tags:

views:

630

answers:

3

i have a linkbutton on a page, the event hander for the click event of that button calls a method on an object which in turn makes a call to a webservice, when the webservice returns the object fires an event and an event handler on the page codebehind updates a label on the page. For some reason the label doesn't display the message.

This has been driving me crazy and i can't find anything about it on google. any help would be appriciated.

The webservice is being called asynchronously but the thread is being blocked until it returns. and the labels pre render event is being called after its text is being set by the proxy objects event handler.

All the calls are happening on the same thread, any values i set ,not just the label text, in the event handler are getting reverted to their previous values by the time the pre-render event fires.

A: 

Have you got this in your Page_Load event?

if (Page.IsPostBack)
{
   return;
}

If not, the label can be re-initialized after the postback.

faircloc
A: 

Is the webservice running asynchronously? Maybe the webservice complete event fires after the page has been rendered? Have you debugged to make sure the label value is being set?

Chris Hynes
+3  A: 

Here's what's happening:

  • The user clicks your link button
  • The browser sends a postback request to the web server
  • The web server creates a brand new instance of your page class and starts a new page life cycle
  • The page life cycle does all it's normal load work.
  • The page life cycle reaches the event handling stage and runs your click event
  • The click event invokes the web service via the "proxy" object you described
  • The proxy object calls the web service asynchronously and returns
  • The click event returns, because it has nothing else to do, freeing up ASP.Net to continue moving through the page life cycle.
  • The page life cycle finishes, so the page is rendered to the browser
  • The user sees the new page, but the label is the same because the web service call hasn't finished yet
  • Web service call returns to your proxy object, which raises its completed event
  • Your event handler updates the label's text property in your page class which was already rendered
  • Your page instance from this request is disposed.

Update:
The OP updated his question after I posted this to indicate that the life cycle timing is kept in sync. So instead, I'll point out that controls in ASP.Net are not threadsafe, but the webservice completed event is called from the thread used in the background to make the asynchronous request rather than the originating thread. You should Invoke the control when setting the property.

Joel Coehoorn
+1 for explaining async in such detail :)
annakata
erm, how would I Invoke a Label control?
Rob