Andre de Cavaignac

Let's blog it out...

Classic ASP Code With .NET Flavor

Recently, I was playing with archive.org and came across some old blog postings of mine.  These postings are about ASP classic (you know, that old stuff that no one really pays attention to anymore)....  Long after learning .NET, I was charged with rewriting a site (from scratch) in ASP.  I typed "IsPostBack" and of course was told that was undefined.  Doh!  So I did a few things to make ASP a bit more .NET-like.  Anyone still writing in ASP but used to .NET may want to take a look at this!

Making Classic ASP Feel Better (Wednesday, March 02, 2005)
So overall, classic ASP sucks.  I hate it, I don't like touching it, its ruining my C# coding habits, and I'm about to kill someone because I have to code in it.  That being said, I found a way to make classic ASP feel a little bit better: .NET it.

Currently, I am working on a project which is implemented in ASP.  Because the code base is ASP, I need to build this new module in ASP.  Building new things in classic ASP makes me cringe, the thought of mixing code with HTML and having to possibly migrate to .NET in the future is disasterous.  That said, I have found a way to make it bearable:  Make your own .NET framework (so to speak).

After becoming completely fed-up with the ASP coding style, I decided to implement a bit of a framework of my own.  Events that are triggered on the client and processed by functions, and a clean “code-behind” style of coding.

Two includes (framework.inc.asp and framework_top.inc.asp) make up the majority of the framework and are quite simple.  I'll show an example below:

 

MyPage.asp
<html>
<body>
<form id=“Form1” method=”post”>
<!--#INCLUDE file= “MyPage.inc.asp”-->

Click this button for a suprise:<br>
<input type=“button“ onclick=“onEvent(this.name,'onClick')“ name=“mybutton“ value=“Click me“><br>
<%= StrVisibleVar %>

</form>
</body>
</html>

 

 

--------------------------------------------------------------------------------


MyPage.inc.asp
<!--#INCLUDE page=“framework_top.inc.asp”-->

<%
' The variable that will hold the string that will be outputted if the button is clicked
Dim StrVisibleVar

' Occurs on page load
Function Page_Load()
    'Do Stuff
End Function

' Occurs when the button is clicked
Function myButton_onClick()
    StrVisibleVar = “The server says you have clicked the button“
End Function
%>

<!--#INCLUDE page=“Framework.inc.asp”-->

 

 

--------------------------------------------------------------------------------


Framework_top.inc.asp
<&
' This function is only here if it is never replaced by one in MyPage.asp
Function Page_Load()
  ' This will never trigger
End Function
%>

 


--------------------------------------------------------------------------------

Framework.inc.asp
<%
' Holds the value stating if this is a postback
Dim IsPostBack
' Not sure on this code
IsPostBack = (request.HttpParams(“HTTP_METHOD“) = “POST“)

' First call page load
call Page_Load()

'Now check if an event fired.
If (IsPostBack = true) And ( request(“eventOccured“) <> ““ ) Then
   'Trigger the event.
   execute( “call “ & request(“eventOccured“) & “()” )
End If
%>

<!-- This will be appended in the form to handle postbacks. -->
<script language=”Javascript”>
   'Submit the event to the server (do a postback)
   onEvent( triggerName, triggerEvent )
  {
      document.forms[0][“eventOccured“].value = triggerName + “_“ + triggerEvent;
      document.forms[0].submit();
  }
</script>

<input type=”hidden” name=”eventOccured”>

 

--------------------------------------------------------------------------------


Most of this is pseudo-code, as I recreated it from the top of my head, but you get the idea.  Its pretty cool to implement, and gives you a bit of .NET style coding and view-abstraction from your code.

 

Saving ASP VBScript "Objects" to Session (Tuesday, March 08, 2005)
Continuing my trouble and hatred of anything todo with Visual Basic, especially VBScript.  In this episode of The VB Sucks Edition, I'll reivew the problem with VBScript "Objects”.  As any programmer knows, VBScript was never meant to be object-oriented, but by VBS 5, someone at Microsoft thought they could try to make it a bit OO compatible.  The introduction of VBS classes was a major step in that direction.  This seems like a good idea, but instead of making real objects, Microsoft hacked together classes that really mean nothing.  Let me explain.

VBS objects are not really stored in memory as objects.  For this reason, it is not possible to store a VBS object in the Session or Application variables in ASP.  This creates huge headaches, and is completely rediculous.  There are however, some solutions.

The first solution is "Serializing" your class.  You will not truely be serializing anything, but rather creating a "seralize" and "deserialize" method for each class that you declare.  These methods will take a Session variable name as a parameter and "store" or "retrieve" a class from session.  See the example below.

Class MyClass
     Dim Var1
     Dim Var2
End Class

Function Serialize_MyClass( instance, sessionVar )
     Session( sessionVar & ".Var1" ) = instance.Var1
     Session( sessionVar & ".Var2" ) = instance.Var2
End Function

Function Deserialize_MyClass(sessionVar)
     Dim instance : Set instance = new MyClass
     instance.Var1 = Session( sessionVar & ".var1" )
     instance.Var2 = Session( sessionVar & ".var2" )

     Set Deserialize_MyClass = instance
End Function

This solution will work if you must keep all your code in VBScript, however is quite a pain to implement.  If you can mix JScript into your code, I suggest using the following solution.

The other way of saving objects to session in ASP is not to use VBScript objects, but use JScript objects instead.  Because of the way JScript creates objects, you can save JS objects to Session.  By declaring your class in JS, you can keep your code (somewhat) clean, and still have the ability to save your objects.  See below for an example.

<script runat="server" language="jscript">

function MyClass()
{
   this.Var1 = Var1;
   this.Var2 = Var2;
}

var Var1, Var2;

function MyClassFactory()
{
   return new MyClass();
}

</script>

Using script like the one above allows you to keep your class declarations clean, and removes the need for "serialization” functions.  The only problem that still remains is VBScript can't declare instances of JScript classes (even though it can use them).  For this reason, I suggest using a factory function in JScript for your classes (such as MyClassFactory in the example above).  Because VBScript can call JScript functions, you will be able to get a new instance of the JScript class into your VB code, as shown below.

Dim instance : instance = MyClassFactory()
instance.Var1 = "12345"


So there you have it, the half-ass hack to solve the problem of saving classes to Session or Application variables to the piece of junk that is VBScript.

Leave a Comment

(required) 

(required) 

(optional)

(required)