问题
I have a dataview with around 2000 rows. At page load I want to bind the dataview to an object datagrid :
dataGrid.DataSource = dv;
dataGrid.DataBind();
However the time response is very high (a couple of minutes). What can I do to make it faster ? Or at least can I not freeeze the client ?
回答1:
Check your query first. Once you have it as fast as you can get it (including possibly pre-caching data nightly in a temp table or something) then move to the ASP.NET code and make that faster.
Turn off viewstate for your grid if you can. The viewstate will increase your page size dramatically. Part of your problem will be just serving up the MB's of raw HTML and viewstate and then rendering it.
Steps I would take:
- Turn off viewstate for the grid
- If you are using templates in your columns, try and trim them down by combining controls to reduce the amount of duplicate binding that is occuring.
- Use literal controls in your templates when possible (significantly lighter than labels)
- Pull out all the styling and make sure you you use css to bring down the page size as well.
- If your rows have any javascript, consider removing any inline scripts and applying it once the page loads via Jquery or some other method.
- Think about paging your data
You can get the 2k+ records to work on one page but you will have to make things very tight to do so.
Last resort, get rid of the grid and just use a literal control and output raw, clean, tight html directly to it. Make sure to turn off viewstate for the literal control in this case as well.
回答2:
Check how long it takes to return data from the dB. If it isn't too long, the problem is probably down to the page attempting to render 2000 rows.
If so, paging will speed things up.
回答3:
I'd reduce the number of items you are bring back in your query with some type of default filter. That much data on the screen is USUALLY going to make for a bad user experience anyway and the default filter would speed up the rendering of the page. If the user asks for 2000 rows of data then let them have it...but I'd avoid doing paging with the datagrid because the paging discards the records that aren't in the current "page" after it executes the query and brings back all 2000 records. So if set the paging to 20 items, the query will still bring back all 2000 records and the grid will remove the other 1980 records each time the user asks for a new page...
来源:https://stackoverflow.com/questions/1742421/asp-net-binding-big-dataview-to-datagrid