.Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old April 29th, 2008, 10:06 AM
baddy's Avatar
baddy baddy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: St. Louis, MO, 63122
Posts: 268 baddy User rank is Sergeant (500 - 2000 Reputation Level)baddy User rank is Sergeant (500 - 2000 Reputation Level)baddy User rank is Sergeant (500 - 2000 Reputation Level)baddy User rank is Sergeant (500 - 2000 Reputation Level)baddy User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 1 h 23 m 32 sec
Reputation Power: 26
Send a message via MSN to baddy
Question C#.Net - issue with how function updates page

I have a page with a few dropdowns - whenever you select a item in one of the dropdowns, it runs a function in the codebehind and updates the page accordingly.

The problem I am having is part of the function is to run a SQL query that is slow (5 seconds or so), and it seems like it waits until everything in the function is competed before it updates the screen. For example I have something like this:

Code:
protected void frame_load()
    {
        lit1.Text = "Loading...";

        // big sql query that takes ~5 seconds

        lit1.Text = "Done!";
    }


I was hoping to have the page display "Loading..." while it ran the long query, but the page just sits there for 5 seconds and then displays "Done!".

Any ideas on what I could do to get around this? I am using C# and AJAX in Visual Web Developer 2008 Express.

Reply With Quote
  #2  
Old April 29th, 2008, 10:57 AM
f'lar's Avatar
f'lar f'lar is offline
Senior WeyrLeader
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Aug 2003
Location: WI
Posts: 3,782 f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 3 Days 22 h 12 m 27 sec
Reputation Power: 675
Send a message via Google Talk to f'lar
Quote:
it seems like it waits until everything in the function is competed before it updates the screen
That's exactly what it does. Well... sort of.

If you're under the notion that your code ever updates the screen directly you need to update your understanding of how this whole 'web' thing works. Your C# code runs far away on a web server, and has no access to the user's screen. All your code does as far as the screen goes is send html to a browser. That's it. That's the way it's always been, and nothing will change it any time soon. I realize it's tempting to believe that setting a property in your page causes some magic communication to happen immediately between your server and the web browser that syncs everything up, but that's not how it works.

Each time your code runs corresponds to exactly one request from the browser. Your program will react by sending exactly one response, usually html. The result of this response may mean the browser makes additional requests for css or javascript files, images, and other resources, but those requests aren't usually passed through the ASP.Net processor. Since your program sends exactly one response, it should be obvious that this response isn't sent until all the relevant code has completed.

Hopefully when you look at it that way your problem starts to make more sense. At the point where you set the 'loading/done' text your code is still deciding what it's html response will look like. lit1.Text = "Loading..."; just means that when the html is rendered you'll have a tag that looks something like this:
<span id="lit1">Loading..."</span>
But since your code isn't finished yet you haven't actually sent that to the browser. It's still just a placeholder. The ASP.Net processor will now move to the next line and run your sql query, but still nothing is sent. Then we reach the last line, where you tell the processor that you really want the span tag to look like this:
<span id="lit1">Done!</span>
At this point, assuming you have nothing else to do in the page lifecycle for this request, your page is finally rendered into html and sent to the browser. So, of course, all the browser ever sees is "Done!".*

AJAX can make this process a little shorter-- it may only run one method or rebuild a single panel instead of rebuilding the entire page-- but it's still a 1:1 request:response ratio.

So how to solve your problem? Well, what I would normally do is show the "Loading..." message using javascript before the the page is submitted back to the server to run the query. This way the user gets a faster update and you only need one response from the server.

To make that work you need to know the ID of your drop down list control so you can find it in javascript using document.getElementById();. Depending on what's going on this ID often isn't fully determined until run time, so you'll need to add the javascript using the ClientScriptManager object in your page and reference the control's .ClientID property to plug the correct ID into your script.

After that your server-side code can just set the lit1 label to "Done!" when the sql query finishes.


---------------------------
*As a side note here, if you don't really need a span tag for this text you can use a literal control instead, which will just send the word "Done!" to the browser with no tag. Of course, then there's no ClientID you can use directly, but that often doesn't matter. There's a good chance it's already encased in something appropriate.
__________________
Primary Forums: .Net Development, MS-SQL, C Programming
VB.Net: It's not your father's Visual Basic.

[Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

Last edited by f'lar : April 29th, 2008 at 02:54 PM.

Reply With Quote
  #3  
Old May 2nd, 2008, 03:52 PM
baddy's Avatar
baddy baddy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: St. Louis, MO, 63122
Posts: 268 baddy User rank is Sergeant (500 - 2000 Reputation Level)baddy User rank is Sergeant (500 - 2000 Reputation Level)baddy User rank is Sergeant (500 - 2000 Reputation Level)baddy User rank is Sergeant (500 - 2000 Reputation Level)baddy User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 1 h 23 m 32 sec
Reputation Power: 26
Send a message via MSN to baddy
Question

Quote:
Originally Posted by f'lar
I realize it's tempting to believe that setting a property in your page causes some magic communication to happen immediately between your server and the web browser that syncs everything up, but that's not how it works.


Hi, thanks for taking the time to reply. I do have a understanding of how the internet works, and the reason I was looking for this certain result is because it is something that I have seen and done before (without magic!). For example, you can write a loop in PHP that will print one line and then sleep for 100ms - when loading it you can see the page load by each line appearing one by one as it is processed. That is where I was coming from when looking for this result, sorry if it seemed so off base.

I tried to have the loading text get updated on the client side, but I am having trouble figuring out how to trigger both the client side and the server side request. I am using OnSelectedIndexChanged as the event, and I can only specify one function (the clientside or the codebehind) to run.

Does anyone know of a way to trigger two seperate functions with one event?

Reply With Quote
  #4  
Old May 2nd, 2008, 03:56 PM
f'lar's Avatar
f'lar f'lar is offline
Senior WeyrLeader
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Aug 2003
Location: WI
Posts: 3,782 f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level)f'lar User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 3 Days 22 h 12 m 27 sec
Reputation Power: 675
Send a message via Google Talk to f'lar
The easiest way to do this is to add a custom validator control that validates your dropdown list. Then set your javascript function as the client-side javascript to run (make sure it returns true) and do nothing on the server side. But that's kind of a crude hack.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > C#.Net - issue with how function updates page


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway