Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Page Transition Class - ASP to ASP.NET Migration

0.00/5 (No votes)
4 Jul 2005 1  
Class that allows the developer to pass session like information back and forth between ASP.NET and ASP pages.

Introduction

The point of this class is to help with migration of large ASP projects to ASP.NET.

The main purpose is to allow the developer to pass session like information back and forth between ASP.NET and ASP pages. I use this class in conjunction with a couple of other classes which I included in the source code. One is a persistable collection, which is really nothing more than a collection that I hold in viewstate.

Background

First, let me explain my reasoning for creating this class. Recently, I was given the task of upgrading a rather large ASP application to an ASP.NET application, but there were a couple of catches (always are).

  1. The updates had to go in phases.
  2. All of the pages pass certain values around to all other pages (via JavaScript client side posts).
  3. Sessions could not be used because multiple browser windows might sometimes be used, causing overwriting of session variables.

To elaborate, every link, button, etc. called a function that the client side posted to either the current page, or another page, and passed hidden form field values (I didn't design it!). The phased development approach made it so I couldn't rewrite the entire site to be ASP.NET, instead I had to do a few pages at a time based on requirements. So I sat down and thought it out and came up with this transition class.

Design

This class was made specifically for this particular web application, so that way you may or may not need to use certain things I've included with it. For example, instead of using viewstate directly, I created a class I call a hidden list. Basically all this class is, is a collection that is viewstate enabled so it will persist through postbacks. I mention this because one of the overloaded add methods for the page transition class allows me to pass it the entire hidden list, allowing me to add everything I need to pass in one shot.

Dim objTrans as New PageTransition("somepage.asp")
objTrans.Add(myHiddenList)
objTrans.Go

I will touch on the hidden list briefly, but it will be included in the source for you to examine.

Code - .NET Side

Private _objPassedValues As Hashtable

Private Sub SaveSessionState()
    Dim strSessionID As String = HttpContext.Current.Session.SessionID.ToUpper
    Dim intRnd As Integer = (New Random).Next
    strSessionID += intRnd.ToString

    HttpContext.Current.Session.Clear()

    For Each strKey As String In _objPassedValues.Keys
        Dim objConn As New SqlConnection(GenerateSqlConnString)
        Dim objCmd As New SqlCommand("spSetSessionState", objConn)
        objCmd.CommandType = CommandType.StoredProcedure
        objCmd.Parameters.Add(New SqlParameter("@SessionID", strSessionID))
        objCmd.Parameters.Add(New SqlParameter("@Variable", strKey))
        If TypeOf _objPassedValues(strKey) Is Boolean Then
            objCmd.Parameters.Add(New SqlParameter("@Value", _
                            _objPassedValues(strKey).ToString))
        Else
            objCmd.Parameters.Add(New SqlParameter("@Value", _
                                     _objPassedValues(strKey)))
        End If


        objConn.Open()
        objCmd.ExecuteNonQuery()
        objConn.Close()
    Next

    If _strDestination.IndexOf("?") <> -1 Then
        _strDestination += "&TranSID=" & strSessionID
    Else
        _strDestination += "?TranSID=" & strSessionID
    End If
End Sub

As you can see, I got the main idea of this from cookieless sessions with SQL Server. (The stored proc is included in the source also.)

Basically what I do here, is this:

  1. Get the current session ID.
  2. Get a random number and append it to the session ID.
  3. Immediately clear the session to force a new session ID to be generated.

This keeps the ID unique for this browser. Then all it does is simply append the destination specified with a querystring value.

The class doesn't Response.Redirect until you execute PageTransition.Go. The reason for this is, because there is a method called SimulateGo which instead of actually transitioning, does everything else and returns where it would have gone. You may think I'm nuts, but I actually had a reason for it.

Here's what the Go method looks like:

Public Sub Go()
    Dim objSession As SessionState.HttpSessionState = HttpContext.Current.Session
    For Each strVariable As String In objSession.Keys
        Try
            _objPassedValues.Add(strVariable, objSession.Item(strVariable))
        Catch
        End Try
    Next
    SaveSessionState()
    HttpContext.Current.Response.Redirect(_strDestination)
End Sub

As you can see, it's fairly simple. It grabs all values out of session at the last second (since they're cleared in SaveSessionState) and then saves the session to the database. And lastly it redirects.

Code - ASP Side

This code is fairly simple. In our case, we had a dictionary object on every ASP page that held all of the form values (don't ask me why, not my design!). So I simply put any transitioned information directly into that, but you can do as you see fit.

Sub subHandleDotNetTransition
Dim strTransitionID, objTransitionRS, strReferer
    strReferer=Request.ServerVariables("HTTP_REFERER")
    if instr(strReferer,"?") > 0 then
        strReferer= left(strReferer, instr(strReferer,"?")-1)
    end if
    If UCASE(Right(strReferer, 5)) = ".ASPX" Then
        strTransitionID = Request.QueryString("TransID")
        If strTransitionID <> "" Then
            'Execute your DB code to call spGetSessionState and retreive

            'the session information by passing it the SessionID

        End If
    End If
End Sub

In my case, this code was executed in a header file that was included on every page in the site.

Code - Hidden List

I still think the hidden list is a neat little tool. Anyway, here're the basics of it.

It's simply a hashtable, that I turned into a control, that has viewstate enabled.

What does that mean? Well, it means that I can store any serializable object in it, and it's preserved till I leave the page. (No more hidden variables!) I've included the source for it, but I won't bother posting its code, it's very simple really.

Conclusion

This was certainly a learning experience for me, but all in all, it worked rather well so I'm pleased. I hope this helps anyone else out as much as it's helped here. This is my first article, so please be kind. Also, I'm on here pretty frequently, so feel free to ask any questions or anything, I'll get back to you rather quickly.

History

  • v1.0 - 2005.06.29 - Created.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here