Optimizing Your Asp.Net Pages for Faster Loading and Better Performance.
If you read the internet and all of the websites dedicated to Asp.Net you will inevitably read about the wonders of the DataGrid, DataList, and Repeater controls. While each of these has its place, if you are only displaying data there is a much faster and more efficient means to do so.
Let’s say you have a page that displays articles based on a query string. Take my article pages for instance. Each article is stored in a database and displayed on the page based on the unique id of the article as stored in the database.
A normal asp page execution procedure goes something like this. The code queries the database on the article ID and then brings back that information to the page where you show it in the fashion that you want. This is a fairly straight forward approach with asp and is done all the time.
So how do we speed up our asp.net pages?
Number 1: Use Asp.Net Caching!
This is a breeze, and I'm not in the details and vibrancy of asp.net caching here because at the time when the letter of this article go 2780000 Google for more. Basically, instead of loading the query the database each time the page, one has only one query of the database and load that into the system cache. Subsequent calls to load the page retrieve the data from the cache to the database that have an immediate and significant performance enhancement compared. You can then use the cache, how long the cache should store and many other features. If you use the cache, you should, if possible!
Number 2: If possible, do NOT use the standard Asp.Net controls.
This is correct. The standard asp.net controls, aimed at rapid development, rather than pages. They allow you to design web pages crawled and display the data very fast, but its actual performance, because additional overhead, which is where the convenience, speed up development time, rather than the page execution speed are affected.
Instead, create either a User Control or even better yet a Web Custom Control which is by far the fastest performance wise and really quite easy to create and use.
Number 3: Use an SqlDataReader or even better yet use a set based command for Sql Server data retrieval and simply execute that one command against the database.
A asp.net SQLDataReader throw is a rapid development of the closure of the only DataReader connection, read the results of the final set. Now, my article pages we have only to return a specific result. In this case, we will choose based on the set command. If you have more than one result returned, in your directory, for example, you can use the SqlDataReader because you have to return multiple result sets.
Set based commands are stored procedures that bring back data through parameters as opposed to a result set which then in turn needs to be looped through to obtain your data. So instead of writing your stored procedure like the following which brings back 1 result set:
Select Title, Body, Author
From Articles
Where ArtID = 215
We can write it using a set based command like this.
Create Procedure mysp_GetArticle
@Title varchar(200) Output,
@Body varchar(8000) Output,
@Author varchar(500) Output
As
Select @Title = Title, @Body = Body, @Author = Author
From Articles
Where ArtID = 215
GO
Since then, the record is returned, it just results in only one set must walk in the query results are just not above three parameters instead of one, the result is returned or the record set called . Because this work it must be possible to avoid the poor performance of two second small process. asp.net caching and combines this approach.
Number 4: Use Classes and ArrayLists as opposed to returning an SqlDataReader.
Create a class and then if there are more than one set of results store those results into individual instantiations of that class. Finally store each of those classes into an ArrayList. You can then store only that ArrayList into the asp.net cache. So instead of getting the results back from a SqlDataReader when loading your page you get them from the ArrayList which is stored in the cache. Nice huh?
Finally… you want to incorporate all of these techniques into your final results which would be performed in the following manner and sequence.
On the first time the page loads, query the database and delivers it to save all your data into separate classes. Then store each class in an ArrayList. If you have only one result you can only save the class in the cache. Then take your ArrayList and store it in the cache.
Next create a Web custom controls and custom controls and data very quickly, HtmlTextWriter to use the loop, pass the cached ArrayList. Load the page, called from class to store the cache for each subsequent call to remember, your one or two classes ArraList.
Certainly, a significant amount of additional coding takes to do it that way, especially if you have a proper error handling into consideration, but when you approach after that your pages be screeching fast, you will immediately notice the difference and your asp. net sites are in the correct order are carried out – data processing in the PageLoad function and the html display in the PageRender function.
Further, you will be glad you did and so will your visitors.
Happy Programming!

Leave a Reply