Click here to Skip to main content
6,292,426 members and growing! (10,212 online)
Email Password   helpLost your password?
Web Development » ASP » General     Intermediate

Classic ASP Framework - Make your Classic ASP code work like in ASP.NET

By Christian Calderon

Developing in Classic ASP using the same technics as in ASP.NET?. If you have to work in Classic ASP, why don't do it the right way?. By using a similar framework not only your code will be more organized and efficient, but it will take you a fraction of the time to port to ASP.NET!
Windows, ASP, Dev
Posted:1 Feb 2004
Views:142,233
Bookmarked:65 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
29 votes for this article.
Popularity: 6.58 Rating: 4.50 out of 5
1 vote, 3.6%
1

2
1 vote, 3.6%
3
3 votes, 10.7%
4
23 votes, 82.1%
5

Introduction

With the Classic ASP Framework you will be able to structure your ASP pages pretty much the same way you would do it in ASP.NET. The beauty of the Framework is that it includes most of the controls found in ASP.NET such as the TextBox, Label, GenericHTML, DropDown, CheckBox, CheckBoxList, RadioButtonList,DataRepeater, DataList, DataTable and MORE! The Framework is event driven and supports ViewState!

Background

So, why did I take the time to do this, you may ask, specially when ASP.NET is so great and does everything you need. Well, the main reason is because I work as a consultant and there are still quite a bit of ASP websites around that require maintenance and support. After I learned .NET I felt in love with it and I did not want to code in any other way anymore. So I decided to take the most important pieces of it and create a similar one on Classic ASP.

The main benefits are:

  • Coding takes a lot less time, is more readable and easier to maintain.
  • Event driven model.
  • Simplifies the migration to ASP.NET.

Screenshots

Using the code

Using the code is pretty simple. Just include the "WebControl.asp" file and a reference to each Server Control (each one is wrapped in its own ASP file) that you want to use to your ASP page. After you include the references then you need to write code inside each event you want to handle.

The Form is taken care of by the Framework. Just include FormStart.asp when the form starts and FormEnd.asp when you need to clode the form tag. This two includes will make sure that all necessary hidden fields are included. The last thing that has to be done is to the Main() method (included in the WebControls.asp). This method will do the magic. I call it the "Page Controler" because it coordinates the execution of all the events in the appropriate order. This method should only be called after the declaration of all the includes and BEFORE rendering any control.

The Framework includes several examples that show how it works.

WebControl.asp contains the core classes: WebControl and Page. All controls "inherit" from WebControl (including the Page class). Now, wait a second, inherits in VbScript?. Not really, I "simulated" inheritance by having each WebControl variable inside each Server Control.

In addition to this, you need to register one COM Comnponent (ASPFramework.dll) which is used to encapsulate the ViewState (which is XML), the Base64 functions and the ListItemCollection. It is a VB Class and the source code is provided. The only reason for using a VB Class is for speed purposes. We don't want to encrypt/decrypt in VBScript!.

**List of supported events that can be overloaded (they don't need to be defined!)

  • Page_Authenticate_Request
  • Page_Authorize_Request
  • Page_Init
  • Page_Controls_Init 'Ocurrs just once, when the page is first loaded!
  • Page_LoadViewState
  • Page_Load
  • Page.HandleClientEvent
  • Page_PreRender
  • Page_SaveViewState

For Server Controls

  • OnInit
  • OnLoad

<!--#Include File = "..\WebControl.asp"        -->
<!--#Include File = "..\Server_LinkButton.asp" -->
<!--#Include File = "..\Server_CheckBox.asp" -->
<!--#Include File = "..\Server_DropDown.asp" -->
<!--#Include File = "..\Server_Label.asp"    -->
<!--#Include File = "DBWrapper.asp"    -->

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>DropDown Example</TITLE>
<LINK rel="stylesheet" type="text/css" href="Samples.css">
</HEAD>
<BODY>
<!--#Include File = "Home.asp"        -->
<%
  Call Main()
%>
<Span Class="Caption">DROPDOWN EXAMPLES</Span>

<!--#Include File = "..\FormStart.asp"        -->
  <%lblMessage%><HR>
  <%chkHideShow%> | <%chkAutoPostBack%> | <%chkListBox%><HR>
  <%cboDropDown%>
  <HR>
  <%cmdAdd%> | <%cmdRemove%>
  
<!--#Include File = "..\FormEnd.asp"        -->

</BODY>
</HTML>

<%  'This would normaly go in a another page, but for the sake 

    'of simplicity and to minimize the number of pages

    'I'm including code behind stuff here...

  Dim lblMessage
  Dim cmdAdd
  Dim cmdRemove  
  Dim chkHideShow
  Dim chkAutoPostBack
  Dim chkListBox
  Dim cboDropDown
  
  Page.DebugEnabled = False
  
  Public Function Page_Init()
    Set lblMessage = New_ServerLabel("lblMessage")
    Set cmdAdd = New_ServerLinkButton("cmdAdd")
    Set cmdRemove = New_ServerLinkButton("cmdRemove")            
    Set chkHideShow = New_ServerCheckBox("chkHideShow")
    Set chkAutoPostBack = New_ServerCheckBox("chkAutoPostBack")
    Set chkListBox= New_ServerCheckBox("chkListBox")
    Set cboDropDown = New_ServerDropDown("cboDropDown")
  End Function

  Public Function Page_Controls_Init()
    cmdAdd.Text = "Add"
    cmdRemove.Text = "Remove"

    lblMessage.Control.Style = "border:1px solid blue;
      background-color:#EEEEEE;width:100%;font-size:8pt"    
    lblMessage.Text = "This is an Example"
    chkHideShow.Caption = "Hide/Show List"
    chkHideShow.AutoPostBack = True
    chkAutoPostBack.Caption = "DropDown AutoPostBack"
    chkAutoPostBack.AutoPostBack=True
    chkListBox.Caption = "Make it a list box"
    chkListBox.AutoPostBack = True
    
    cboDropDown.DataTextField = "TerritoryDescription"
    cboDropDown.DataValueField = "TerritoryID"
    Set cboDropDown.DataSource = GetRecordset(
      "SELECT TerritoryID,TerritoryDescription FROM
         Territories ORDER BY 2")    
    cboDropDown.DataBind() 'Loads the items collection 

      '(that will stay in the viewstate)...

    Set cboDropDown.DataSource = Nothing 'Clear

    cboDropDown.Caption = "Territory:"
    cboDropDown.CaptionCssClass = "InputCaption"
        
  End Function
  
  Public Function Page_PreRender()
    Dim x,mx
    Dim msg 
    Set msg = New StringBuilder
    msg.Append "<B>Selected Value=</B>" & 
      cboDropDown.Items.GetSelectedText  &  "<BR>"
    msg.Append "<B>Selected Text=</B>" & 
      cboDropDown.Items.GetSelectedValue  &  "<BR>"

    msg.Append "<HR>"
    mx = cboDropDown.Items.Count -1    
    lblMessage.Text  = msg.ToString()    
  End Function


  Public Function chkHideShow_Click()
    cboDropDown.Control.Visible = Not cboDropDown.Control.Visible
  End Function

  Public Function chkAutoPostBack_Click()
    cboDropDown.AutoPostBack = chkAutoPostBack.Checked
  End Function

  Public Function cmdAdd_OnClick()
    cboDropDown.Items.Add cboDropDown.Items.Count,
      cboDropDown.Items.Count,False
  End Function

  Public Function cmdRemove_OnClick()
    cboDropDown.Items.Remove cboDropDown.Items.Count-1
  End Function

  Public Function chkListBox_Click()
    if chkListBox.Checked Then
      cboDropDown.Rows = 10
      cboDropDown.Multiple = True
      cboDropDown.Items.Mode = 2
    Else
      cboDropDown.Rows = 1
      cboDropDown.Multiple = False
      cboDropDown.Items.Mode = 1
    End If
  End Function    
%>

Points of Interest

Developing the core took me about 4-5 days. Each control took me 1-3 hours (with the exception of the DataGrid, which took me a day or two!).

After the whole thing was done I learned quite a few things such as all the tricky things when you restore the viewstate for controls that have a corresponding HTML Input (text boxes, drop downs, etc). They behave differently when they are invisible, disabled, rendered or not rendered... it was quite fun!.

History

  • The current version is 1.3 (production stable)

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

About the Author

Christian Calderon


Member
Christian is a Microsoft Certified Professional 8+ years of experience in the consulting business. He has designed and developed numerous Web and Windows applications (and many other things he doesn't even remember...) using technologies and tools such as ASP.NET,VB.NET, C#, ASP, COM+/MTS, C++, JavaScript, HTML/DHTML, Visual Basic, SQL Server, Oracle, Microsoft Access, etc.

He is quite "curious" and always wants to know how things work and the "why was it done like this or that, how does this works...", and because of this he is always pursuing to improve his skills so he can know the answers to these questions.

He also likes Martial Arts, finishing and spending time with his wife and daughter. And why not?, playing XBOX from time to time :-P

Christian works as the Microsoft Technologies Manager/Senior Developer for Electronic Knowledge Interchange, a Chicago based Technology Consulting Firm.
http://www.eki-consulting.com
Occupation: Web Developer
Location: United States United States

Other popular ASP articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 29 (Total in Forum: 29) (Refresh)FirstPrevNext
QuestionCSS ? PinmemberMember 46743094:53 12 Mar '08  
GeneralI dont suppose this can be run under windows CE? PinmemberMember 391911613:59 17 Feb '08  
GeneralCan it be run without the dll? Pinmemberdavidlandy22:57 11 Jan '07  
GeneralNice framework Pinmembergabru002:07 18 Nov '06  
GeneralIts Great.... PinmemberM Aamir Maniar21:41 19 Dec '05  
GeneralRe: Its Great.... PinmemberChristian Calderon14:52 20 Dec '05  
GeneralHelp! "ActiveX component can't create object: 'ASPFramework.ViewState'" PinmemberNeilMeredith11:33 2 Aug '04  
GeneralRe: Help! "ActiveX component can't create object: 'ASPFramework.ViewState'" PinsussAnonymous5:14 3 Aug '04  
GeneralRe: Help! "ActiveX component can't create object: 'ASPFramework.ViewState'" PinsussAnonymous5:16 3 Aug '04  
GeneralRe: Help! "ActiveX component can't create object: 'ASPFramework.ViewState'" Pinmemberkreont15:48 3 Oct '08  
GeneralRe: Help! "ActiveX component can't create object: 'ASPFramework.ViewState'" Pinmemberlrblouie11:18 3 Nov '08  
GeneralRe: Help! "ActiveX component can't create object: 'ASPFramework.ViewState'" PinmemberEbemcham1:12 1 Dec '08  
GeneralThat fixed it! PinmemberNeilMeredith11:47 3 Aug '04  
GeneralCLASP Sample Site! PinmemberChristian Calderon8:32 14 Jun '04  
GeneralRe: CLASP Sample Site! PinmemberChristian Calderon16:21 10 Oct '04  
GeneralBase64 does not support DBCS characters PinsussAnonymous23:32 12 Feb '04  
Generalnew base64 class Pinsussjiangjianxiao2:00 13 Feb '04  
GeneralRe: new base64 class PinmemberChristianCalderon8:31 13 Feb '04  
GeneralRe: new base64 class Pinmemberjiangjianxiao16:13 13 Feb '04  
GeneralRe: new base64 class PinmemberChristianCalderon17:22 13 Feb '04  
GeneralRe: Base64 does not support DBCS characters PinmemberChristian Calderon12:17 22 Jun '04  
GeneralRe: Base64 does not support DBCS characters Pinmemberplayhere5:28 19 Apr '08  
GeneralNew Version PinmemberChristianCalderon19:56 10 Feb '04  
GeneralRe: New Version PinmemberChristianCalderon4:59 25 Feb '04  
GeneralWell done... PinmembertheJazzyBrain6:05 4 Feb '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Feb 2004
Editor: Nishant Sivakumar
Copyright 2004 by Christian Calderon
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project