|
Speed Up Your ASP.NET Pages
Here are seven ways to boost the performance of your Web pages and sites.
by Dan Fergus
Posted February 24, 2004
Technology Toolbox: ASP.NET
Two and a half years have passed since Microsoft first released ASP.NET. Yet even after that much time and experience with ASP.NET, developers are still asking about performance. The issue comes up constantly in discussion groups and during Q&A time at user groups.
These seven performance tips will help speed up your ASP.NET-based pages and sites. I'm assuming you're being careful with your HTML coding, of course. Otherwise, classic ASP, Java, and even ASP.NET can run poorly.
Users will still think your site is slow if their connection is slow or your Web server is overloaded. Of course, this doesn't let you off the hook. All else being equal, efficient ASP.NET coding makes a difference in what users see as the performance of your Web site. See if these tips don't do the trick for you.
1) Use Page.IsPostBack
You need to take a series of steps when your page loads for the first time to perform setup formatting on the page or initialize data. However, you don't need to keep reinventing this wheel the next time a page is requested—and the next and the next. Avoid initializing the data again on the postback. For example, let's say a Web page pulls data from a Web service concerning information about a news header for the page:
if( !Page.IsPostBack )
{
// Get the class data
dbAccess oWS = new dbAccess();
DataSet ds = oWS.GetNewsInfoDS();
}
This data is static, so it needs to load only on the first execution of the page. You can ensure it doesn't occur more often by using the IsPostBack property of the Page class. You can also cause some elusive page bugs by not using IsPostBack. For instance, your breakpoints might show the expected data in the controls on a page after the user clicks on a button, but when the page is reposted, the data gets reset to the values the controls had before the page was reposted. This glitch stems from the sequence of events that occurs when a page is being posted back to the server.
2) Use SQL Server Stored Procedures for Data Access
SQL Server stored procedures can improve performance for both Windows applications and ASP.NET Web applications. You boost performance when you write and save a command as a stored procedure. SQL Server optimizes that command and stores it in a compiled state, making queries run faster than when you submit them as ad hoc SQL statements. The SQL managed provider also talks to SQL Server in its own language, which shrinks the front-to-back time required to get a query through and data back to your client.
Stored procedures provide data security benefits as well. Hackers can type certain characters into the username and password boxes of your standard login script and magically gain access to your site's database. Fortunately, stored procedures help resist script attacks. It's good practice to forbid queries by anything other than stored procedures, which helps guard your valuable or sensitive data.
Back to top
|