Is it smart thing to create html string on server side and append it on client side?
I need to transfer a large amount of data from server to client and then, with JavaScript (jQuery) generate a lot of tables, div
s and other know and unknown html elements .
When I was trying to generate those tables and div
s on client side a had a couple of for loops and I was creating and appending elements on the fly.
I soon discovered that it takes a lot of time to generate all those elements and show them so I generate HTML on server side with HtmlTextWriter
then I convert it ToString()
and append that string on client side.
It is much faster but it is "seljacki" as we would call it in my language (the closest translation is "not elegant solution").
So I wanted to ask does anyone have some more elegant solution to suggest开发者_开发百科 and is this method that I'm using completely wrong or it could pass?
Thanks to everyone.
You don't show your code here, but I'd guess that in your loop you are repeatedly appending rows to a table or whatnot as you iterate through the results. If so, the bottleneck is not actually generating the HTML but redrawing the content each iteration. Try assembling the entire HTML to add in memory first, then use a single append()
or html()
call to update the document.
I would continue to pass the data to the client side and create the html. I've just started playing around with jQuery templates and so far they are awesome. Since it is obviously going to be a little slower creating the elements on the client side why not just create and/or load them as needed. The user can only see so much at a time so why make them wait for something they may never see.
精彩评论