|
 |
This tutorial shows how to use basic caching to improve the performance of a website. VB version.
This tutorial shows how to use basic caching to improve the performance of a website. VB version.
Download the Full Working Version of this Project written with Visual Studio.NET VB 2005 Here!
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!
We can add the following line of code to the top of our ASPX pages to set a duration that the page is valid for. With the below example, the page will not be reloaded during the 15 second timeframe. This means that if the user was to refresh the page within those 15 seconds, the cache version would be loaded.
| <%@ OutputCache Duration="15" VaryByParam="none" %> | Example: If we had a label that displayed the time (as below), the time would only be updated if the page was reloaded after the 15 second timeframe.
| Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text = System.DateTime.Now.ToString() End Sub | The above example of caching is page-specific. If we wanted to cache pages on a larger scale on our website, we could use Application-Level Caching. This is done by modifying the Web.config file:
<caching> <outputCacheSettings>
<outputCacheProfiles>
<add name="AppCache1" enabled="true" duration="60"/> </outputCacheProfiles> </outputCacheSettings> </caching> | And then modifying the ASPX directive:
| <%@ OutputCache CacheProfile="AppCache1" VaryByParam="none" %> | This directive refers to the Cache Profile specified in the Web.config file. However, there are yet more ways we can use caching. We can cache pages using parameters (VaryByParam). This will enable ASP.NET to store different variations of the page depending on certain parameters such as query strings, post values, request headers, etc. We can create an example of this by changing the color of the label that holds the current time. We create a textbox and a button, and allow the user to type a color to change the label. Every time the color changes, the time should change. However, if the color remains the same, and the page is still within its duration specified in the directive (60 seconds), then the time will stay the same. This is because it is the cached version of the page which is being loaded. So we change the directive once again to add the parameters:
| <%@ OutputCache Location="Server" Duration="60" VaryByParam="textBoxColor" %> | And our ASPX page will look something like this:
<form id="form1" runat="server" defaultbutton="Button1"> <div>
<asp:Label ID="Label1" runat="server"></asp:Label><br /> Type the name of a color: <asp:TextBox ID="textBoxColor" runat="server"> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Change Color" /><br /> <br /> The time displayed in the label should only update if a new color is entered, or the page cache duration has expired (60 secs)<br /> </div> </form> | The code-behind will be something like this:
Imports Microsoft.VisualBasic Imports System Imports System.Data Imports System.Configuration Imports System.Web Imports System.Web.Security Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Imports System.Web.UI.HtmlControls
Partial Public Class _Default
Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text = System.DateTime.Now.ToString() End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Label1.BackColor = System.Drawing.Color.FromName(Server.HtmlEncode(textBoxColor.Text)) If Label1.BackColor = System.Drawing.Color.Black Then
Label1.ForeColor = System.Drawing.Color.White Else
Label1.ForeColor = System.Drawing.Color.Black End If End Sub End Class | Download the Full Working Version of this Project written with Visual Studio.NET VB 2005 Here!
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!
|
|
|