Click here to Skip to main content
12,751,993 members (30,582 online)
Click here to Skip to main content
Add your own
alternative version

Stats

26.9K views
632 downloads
13 bookmarked
Posted 21 Jun 2011

Session and ViewState alternative

, 21 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A great alternative for Sessions and ViewStates in ASP.NET.

Merula Smartpages Logo

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
    //normal asp.net page (Change Page to SmartPage and it will work!)
    {
        private string value;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                value = "<h1>Hello world!</h1>"; //set the value
            }
            else
            {
                Response.Write(value); //wont work, value will be empty
            }
        }
    }
}

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>
                <!--<span class="code-comment"> Button to create a postback --></span>
                <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.

diagram

Merula SmartPages

More information about Merula SmartPages can be found at http://www.merulasoft.nl.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rob Stortelers
Student
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
Armando de la Torre29-Jun-11 14:16
memberArmando de la Torre29-Jun-11 14:16 
QuestionRemimber Pin
Yves27-Jun-11 12:26
memberYves27-Jun-11 12:26 
NewsSourcecode Pin
Rob Stortelers22-Jun-11 8:20
memberRob Stortelers22-Jun-11 8:20 
Question[My vote of 2] Why is it a lot of work to work with Session/ViewState? Pin
JV999921-Jun-11 22:14
memberJV999921-Jun-11 22:14 
AnswerRe: [My vote of 2] Why is it a lot of work to work with Session/ViewState? Pin
rManiks6-Jul-11 0:40
memberrManiks6-Jul-11 0:40 
QuestionNeed source code. Pin
Vinayak Singh Rathore21-Jun-11 22:03
memberVinayak Singh Rathore21-Jun-11 22:03 
GeneralMy vote of 2 Pin
GilbertoBotaro21-Jun-11 14:45
memberGilbertoBotaro21-Jun-11 14:45 
GeneralMy vote of 2 Pin
Artusik21-Jun-11 10:36
memberArtusik21-Jun-11 10:36 
QuestionI think you need to.......... PinPopular
DaveAuld21-Jun-11 7:23
memberDaveAuld21-Jun-11 7:23 
AnswerRe: I think you need to.......... Pin
wgbarnum21-Jun-11 7:54
memberwgbarnum21-Jun-11 7:54 
GeneralRe: I think you need to.......... Pin
Paulo Zemek21-Jun-11 8:48
memberPaulo Zemek21-Jun-11 8:48 
GeneralMy vote of 5 Pin
mhamad zarif21-Jun-11 6:52
membermhamad zarif21-Jun-11 6:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 21 Jun 2011
Article Copyright 2011 by Rob Stortelers
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid