开发者

How to load and show data Asynchronously

开发者 https://www.devze.com 2023-02-09 05:48 出处:网络
I am using ASP.NET and SQL Server. I would like to load开发者_运维问答 data from database asynchronously and show data which are partially loaded immediately.

I am using ASP.NET and SQL Server. I would like to load开发者_运维问答 data from database asynchronously and show data which are partially loaded immediately.

Suppose there are tons of records in a query result. After 3 sec,It loads 20% then I have to process and show 20% data immediately, not waiting for complete response. I know $.Ajax in jQuery to load data async. Is it possible to process partial response, Not wait for complete response and show it immediately.

Is there any way to get this?


Suppose there are tons of records in a query result

You have to aks yourself: Will the end user see a ton of records at once?


You don't specify what and how you are showing the data, so I'm going to assume that you are showing the data in something like a Grid.

What we do this days on such commun scenario is to load data using pages and fire it when the user scrolls the grid down, you can even see this in mobile devices and even on facebook, Twitter, etc...

It will load the first page (for example we set up the page to be 20 records), so it will load the first 20 records (page 0), but soon you reach the bottom, it will automatically load 20 more records (page 1).

This technique is called Infinite Scroll


Like Alex said you can't start showing a partial response... I bet your wait time is related to the time it takes to query and return the dataset, not the time it takes to render the date. That being said you could easily create the functionality to only get the first 20% of the records before trying to get the last 80%. Of course you would load each result asynchronously.

The downside to this is that you have to generate 2 requests/response cycles and 2 database queries.

Just curious how long does it take to run the SQL query outside of your website (using SQL Manager for example)? How much data is actually being returned 10kb or 1000kb? If you have a lot of text data, look at adding compression, to your responses.


You could use a hidden IFRAME to receive the response, which is written to using the ASP response.flush method (outputs incrementally formed responses). Then, using a js interval, you can query this iframe's body to get the updated content...


Possible solicion:

  1. You create a file ASHX (Generic Handler) or that suits you. This file will receive consultations by GET or POST.
  2. Believe 2 store proceduce a. returns the number of records b. has the full result
  3. Ajax to make the call sp "a". and you will receive the amount of records.
  4. Ajax query to sp "b" and you save a part of the query in cache or session and answer the number of records you want.
  5. Ideally, the outcome of the consultation is in json format and can be smaller, but take longer to load in javascript. You can also return it in HTML, transfer is heavier but much faster to load.

(I hope you understand because my English is pretty bad)


No it`s impossible, you should add several request to process partial response, on server side you should partially data. In $.Ajax (jQuery) option async mean that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.


Here is the solution in simple. 1. you should create a separate asp.net page which will take 1 parameter, starting record number, you should make sure to have a table with unique record number. and retrieve say 20 rows from the given starting record number. 2. You need to call the AJAX get method which is recursive by incrementing the record number each time by 20 till you get the response as NULL.

Hope it will solve.


Instead of showing a partial response, why don't you simply show a server side paged table of data. If you have many, many records, simply show only 10 records on a page, going back to the database to get the next 10 when the user pages the data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号