Introduction
When you want to remember your data in an ASP.NET site after a postback, you will have to use Session or ViewState to remember your data. It’s a lot work to use Session and ViewStates, I just want to declare variables like in a desktop application.
So I created a library called Merula SmartPages. This library will remember the properties and variables of your page.
Using the code
You can download the SmartPages Library here
Using the SmartPages library is very simple. All that you’ll have to do is create a new ASP.NET page and change the extension of the class from Page
to SmartPage
.
using System;
using MerulaSmartPages;
namespace SmartPageTester.smartpages
{
public partial class MyPage : SmartPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
In ASP.NET, this won’t work:
using System;
using System.Web.UI;
using MerulaSmartPages;
namespace SmartPageTester.smartpages
{
public partial class MyPage : Page
{
private string value;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
value = "<h1>Hello world!</h1>";
}
else
{
Response.Write(value);
}
}
}
}
The ASP code:
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="MyPage.aspx.cs" Inherits="SmartPageTester.smartpages.MyPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnHello" runat="server" Text="Say Hello" />
</div>
</form>
</body>
</html>
When you change the class extension from Page
to SmartPage
, the above example will work.
How does it work?
Every user on your site has a unique Session. When a user visits your SmartPage
, the SmartPage
signs in by a PageManager
. The PageManager
is a singleton and will keep the data of the SmartPage
alive. When a postback occurs, SmartPage
will load its data from the PageManager
. With the help of Reflection, all the data from the variables and properties will be filled in.
When SmartPage
is not used within 5 minutes, it will be cleared from the server's memory.
Merula SmartPages
More information about Merula SmartPages can be found at http://www.merulasoft.nl.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.