Groups | Blog | Home
all groups > asp.net building controls > april 2007 >

asp.net building controls : Updating a control with data on server event.


Serendipity
4/18/2007 11:32:01 AM
Hi, I'm new to ASP.NET, so please forgive me if my questions are sometimes
nonsensical.
I've read a few articals and I think I've got down the idea of "user action
on client -> postback -> server event raised and handled -> server renders
page and sends it back to the client" - right? But what I want to do is,
cause the control to be visibly updated, but not as a result of a user
action, but as a result of something that happens on the server (e.g.,
asynchronious action has returned with data). Can my control somehow tell the
server to render the page? Or is there another way to update control
appearance?
Thanks in advance,
John Saunders [MVP]
4/18/2007 5:51:32 PM
[quoted text, click to view]

The way you described it above it the way it works. You just left a piece
out:

"user action on client -> postback -> ASP.NET creates an instance of the
page -> the page creates instances of its controls -> server event raised
and handled -> server renders page and sends it back to the client ->
ASP.NET destroys the page and all controls on it"

So, your control can't tell the server anything, because no instance of your
control exists.

In these situations, one usually has the client make an asynchronous request
to the server. If you Google for "progress bar" and ASP.NET you'll find many
articles on this.
--

John Saunders [MVP]

stcheng NO[at]SPAM online.microsoft.com
4/19/2007 12:00:00 AM
Hi Almog,

As for the "updating control ..." question you mentioned, I think it is a
typical asynchronous server-side processing and update client case, as John
has suggested, generally, you can let the server-side keep processing the
task(in a background thread) and the page will return the client
synchronously. After that, in client page, you can use script to constantly
post to the server and query the processing status, if finished, post back
the page and display final result.

Here are some former thread in newsgroup and tech articles in MSDN library
that discussing on similar topic:

http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet/brow
se_thread/thread/44302efa18ea9567/1619ca7b4000d2f8

#Reporting Task Progress With ASP.NET 2.0
http://msdn.microsoft.com/msdnmag/issues/06/09/CuttingEdge/default.aspx

#How To: Submit and Poll for Long-Running Tasks
http://msdn2.microsoft.com/en-us/library/ms979200.aspx

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.









Serendipity
4/19/2007 1:56:01 AM
Thank you for the explanation! That has really cleared my view on the issue.
I'll do what you suggested.

[quoted text, click to view]
Serendipity
4/19/2007 1:58:02 AM
Thank you for your help!

[quoted text, click to view]
Serendipity
4/21/2007 12:04:01 PM
Hi again,
I've read the articles, and they are indeed very helpful, but I still have a
few gaps that I don't know how to fill. I will describe the situation in more
detail: my control may be one of several controls on a page. The user can use
this control to poll for some data. I was planning to use a timer object. I
want the control fields to be updated with data when data is available. I
don't want the user to see a "wait..." page, I want the user to continue
using the same page.
So I have the asynchroneous opeartion running, and I can have a data store
that would tell me whether data is already available for my session, but I
still don't have an idea how to poll that data store.
Please help...
Almog.

[quoted text, click to view]
Serendipity
4/22/2007 5:04:00 AM
One more question, and again please excuse my ignorance: how does data
binding work? Doesn't the client respond to changes in the data object on the
server?

[quoted text, click to view]
stcheng NO[at]SPAM online.microsoft.com
4/23/2007 12:00:00 AM
Thanks for your Almog,

So you want the user still see the same page when the asychronous operation
executing at server-side and let the client-side constantly poll the
operation status without postback the page, correct?

To do this, you can simply use some AJAX script, here instead of involving
the new ASP.NET AJAX framework, I would suggest you have a look at the
ASP.NET 2.0's built-in client script callback feature. This feature can
help you generate client-side script that call a server-side method in your
page class(defined in codebehind or inline). Here are the related MSDN
reference and sample on using script callback:

#Implementing Client Callbacks Without Postbacks in ASP.NET Web Pages
http://msdn2.microsoft.com/en-us/library/ms178208(vs.80).aspx

#Client-Callback Implementation (C#) Example
http://msdn2.microsoft.com/en-us/library/ms178210(vs.80).aspx

#ICallbackEventHandler Interface
http://msdn2.microsoft.com/en-us/library/system.web.ui.icallbackeventhandler
..aspx

thus, you can define a server-side page method which will access the
certain data storage(such as Session) to check the operation status. And in
client-side, you can use "window.setInterval" to constantly invoke that
script callback function to poll for the status.

#JavaScript >> Window >>setInterval
http://www.devguru.com/technologies/javascript/10904.asp

#Using Timers in JavaScript - Sliding Around
http://www.devshed.com/c/a/JavaScript/Using-Timers-in-JavaScript/5/

Hope this helps further. If you still have any question, please feel free
to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
stcheng NO[at]SPAM online.microsoft.com
4/25/2007 3:08:53 PM
Hi Almog,

Does the further information in my last reply helps you some? Please feel
free to post here if there is anything else we can help.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.



Serendipity
4/26/2007 1:10:02 AM
Certainly!
I'm currently trying to apply that new knowledge... thanks!

[quoted text, click to view]
Serendipity
4/26/2007 5:34:05 AM
Here I am again with those questions...
I figured I actually don't have to poll for the server action, unless I want
to get partial data as it comes. When it ends, it will call the "OnSucceeded"
client script that I registered. Is that correct?
I still want to use setInterval to start off the server action. I'm not sure
it's possible, though.
If I understand correctly, the client script is registered with the page
containing the control. I can use registerStartupScript to call
setInterval(). The problem is I want to be able to change the interval, but
that would be impossible, wouldn't it? I can't change or unregister the
script?
Alternatively I was thinking to start a timer on the server, where I have
better control, and then I will use setInterval to poll the server for new
data.

[quoted text, click to view]
Serendipity
4/26/2007 5:58:01 AM
And one more: Is I view the source of the page with the control, shouldn't I
see the registered script? I can't see it.

[quoted text, click to view]
Serendipity
4/30/2007 3:20:01 AM
I said I will have questions... ;)
I was looking at this ClientScriptManager.RegisterStartupScript function. I
don't understand how this works. The MSDN article:
http://msdn2.microsoft.com/en-us/library/z9h4dk8y(VS.80).aspx says "The
script block added by the RegisterStartupScript method executes when the page
finishes loading but before the page's OnLoad event is raised".
What is the point of calling RegisterStartupScript from the Page_Load
method, as in the example? Wouldn't that be too late? And how can I use it
from a control, is it possible? Otherwise I have to have some HTML element
clicked in order for polling to start.

[quoted text, click to view]
stcheng NO[at]SPAM online.microsoft.com
5/3/2007 10:11:16 AM
Hi Almog,

As for the script callback polling, though you do not need it to do partial
render, you'll still need it to poll the server-side processing status,
don't you? Since the long-run task is running at server-side, and
server-side can not actively notify client, client-side need to use script
to postback(in background) to poll the result, that's why we need it here.

Also, for the Page.ClientScript.RegisterStartupScript, it will output the
registered script functions at the end of your page's <form>
element.(rendered after other form elements have been rendered). You should
be able to find them in the client browser's "View Source" file. Haven't
you been able to find the function?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
AddThis Social Bookmark Button