This tutorial will show you how to Use Cache using ASP.NET 2.0 and VB.NET.
This tutorial will show you how to Use Cache using ASP.NET 2.0 and VB.NET. First, you need to import the System.Web.Caching namespace.
| Imports System.Web.Caching; |
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
First, the Cache Calss is Implements the cache for a Web application. This class cannot be inherited. We use the btnLoginBetter_Click event to do the work. We then call the Class Cache to use Cache.Insert. The methods of Cache.Insert is Overloaded and Inserts an item into the Cache object. Use one of the versions of this method to overwrite an existing Cache item with the same key parameter. After cache login status into memory, we use TimeSpan to destroy login status within one minutes. A TimeSpan object represents a time interval, or duration of time, measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The largest unit of time used to measure duration is a day. Time intervals are measured in days for consistency because the number of days in larger units of time, such as months and years, varies.| Partial Class UseCacheVB Inherits System.Web.UI.Page End Class Protected Sub btnLoginBetter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim sKey As String = tbName.Text + "_IsLogin" End SubDim sUser As String = Convert.ToString(Cache(sKey)) If sUser = Nothing Or sUser = String.Empty Then Dim SessTimeOut As New TimeSpan(0, 0, 1, 0, 0) Else HttpContext.Current.Cache.Insert(sKey, sKey, Nothing, DateTime.MaxValue, SessTimeOut, CacheItemPriority.NotRemovable, Nothing) lbUser.Text = "Hello!Welcome" lbUser.Text = "Sorry,try again in one minutes" End IfReturn Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
The front end Default.aspx page looks something like this:| <asp:label id="Label1" runat="server">UserName:</asp:label> <asp:textbox id="tbName" runat="server" Width="183px"></asp:textbox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="tbName" ErrorMessage="Please Input UserName!!!"></asp:RequiredFieldValidator><br /> <br /> <asp:label id="Label2" runat="server" Width="78px">PassWord:</asp:label> <asp:textbox id="tbPass" runat="server" Width="183px"></asp:textbox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tbPass" ErrorMessage="Please Input PassWord!!!"></asp:RequiredFieldValidator><br /> <br /> <asp:Button ID="btnLoginBetter" runat="server" OnClick="btnLoginBetter_Click" Text="Login" Width="99px" /><br /> <br /> <asp:Label ID="lbUser" runat="server" Width="286px"></asp:Label> |
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
The flow for the code behind page is as follows.| Imports System.Web.Caching Partial Class UseCacheVB Inherits System.Web.UI.Page Protected Sub btnLoginBetter_Click(ByVal sender As Object, ByVal e As System.EventArgs) End Class Dim sKey As String = tbName.Text + "_IsLogin" End SubDim sUser As String = Convert.ToString(Cache(sKey)) If sUser = Nothing Or sUser = String.Empty Then Dim SessTimeOut As New TimeSpan(0, 0, 1, 0, 0) Else HttpContext.Current.Cache.Insert(sKey, sKey, Nothing, DateTime.MaxValue, SessTimeOut, CacheItemPriority.NotRemovable, Nothing) lbUser.Text = "Hello!Welcome" lbUser.Text = "Sorry,try again in one minutes" End IfReturn Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub |
No comments:
Post a Comment