Click here to Skip to main content
15,892,809 members
Articles / Web Development / HTML

ASP EasyForm: ASP.NET-like forms in two minutes

Rate me:
Please Sign up or sign in to vote.
4.30/5 (7 votes)
27 Nov 2008CPOL7 min read 28.8K   171   18  
EasyForm allows you to create ASP.NET-like forms, with state keeping and server side event handling
<!--#include file="encryption.asp" -->
<%
' *****************************************
' ** EasyForm  ver 1.0                   **
' **                                     **
' ** Author : Vincenzo Rossi             **
' ** Country: Naples, Italy              **
' ** Year   : 2008                       **
' ** Mail   : redmaster@tiscali.it       **
' **                                     **
' ** Released under                      **
' **   The Code Project Open License     **
' **                                     **
' **   Please do not remove this header, **
' **   I will be grateful if you mention **
' **   me in your credits. Thank you     **
' **                                     **
' *****************************************

response.expires = 0

Class EFVariable

    public Name
    public Value
    public ControlType
    private AttributesDict
    
    public sub Init (VarName,varType)	
        set AttributesDict = nothing
        Name        = lcase(VarName)
        ControlType = lcase(varType)        
	    cmd="set " & VarName & " = me"
        execute cmd
        
        if ControlType = "literal" then
            value = EF_Decrypt(request.Form(Name))
        else
            value = request.Form(Name)
            LoadState
        end if            
	end sub
    
    private sub LoadState()
        dim attrList 
        dim attrArray
        dim i
        attrList = EF_Decrypt(request.Form(Name & "_attributes"))
        
        if attrList<>"" then
            set AttributesDict = Server.CreateObject("Scripting.Dictionary")
            attrArray = split(attrList,"~")
            for i=0 to ubound(attrArray)-1 step 2
                AttributesDict.add attrArray(i),replace(attrArray(i+1),"&#126;","~")
            next
        end if 
    end sub
        
    public sub SaveState()
        if ControlType = "literal" then 
            Response.Write "<input type=hidden Name=" & Name & " value=""" & EF_Encrypt(Value) & """ />"
        else
           if not AttributesDict is nothing then
                AllAttribString = ""
                for each key in AttributesDict.keys
                    AllAttribString = AllAttribString & key & "~" &  replace(AttributesDict(key),"~","&#126;") & "~"
                next
                Response.Write "<input type=hidden Name=" & Name & "_attributes value=""" & EF_Encrypt(AllAttribString) & """ />"
           end if
        end if	            
    end sub
        
    public sub Bind()
        select case ControlType
            case "text","hidden","password"
		        Response.Write "Name=" & Name
                Response.Write " value=""" & EF_Escape(Value) & """"                         
            case "checkbox"
		        Response.Write "Name=" & Name
			    value=lcase(value)
			    if value="-1" or value="1" or value="true" or value="on" or value="vero" then Response.Write " checked"
            case "button"
                response.write " onclick=""EF_Call ('" & Name & "_onclick')"" " 
                if value<>"" then Response.Write " value=""" & EF_Escape(Value) & """"
            case "generic"
                ' do nothing    
            case "select", "textarea"
                Response.Write "Name=" & Name			    
        end select
        ' Writing attributes
        if not AttributesDict is nothing then
            for each attributeName in AttributesDict.keys
                ' "data" attributes are not written
                if left(attributeName,5) <> "data." then 
                    if varType(AttributesDict(attributeName))<>vbBoolean then
                        ' Normal attributes <attributename>="<val>"
                        Response.Write " " & attributeName & "=""" & AttributesDict(attributeName) & """"
                    else
                        ' Boolean attributes (disabled, readonly) 
                        if AttributesDict(attributeName) = true then 
                            Response.Write " " & attributeName & " " 
                        end if
                    end if
                 end if
            next
        end if
        
    end sub
    
    public sub AddOption(Val)
        select case ControlType
            case "select"
                Response.Write " value=""" & Val & """"
                if IsInList(Val,Value) then Response.Write " selected"
            case "radio"
		        Response.Write "Name=" & Name
                Response.Write " value=""" & Val & """"
                if cstr(Val)=cstr(Value) then Response.Write " checked"
        end select
    end sub
    
    
	public Property Get Attributes(Key)
	    if AttributesDict is nothing then
	        Attributes = ""
	    else
	        IndexedKey = lcase(replace(Key,"[value]","[" & me.value & "]"))
		    Attributes = AttributesDict(IndexedKey)
		end if
	End Property

	public Property Let Attributes(Key,val)
	    key = lcase(key)
	    if Key = "" then exit property
	    if AttributesDict is nothing then
	        set AttributesDict = Server.CreateObject("Scripting.Dictionary")
        end if
        if AttributesDict.exists(key) then
            AttributesDict(key) = val
        else
            AttributesDict.add key,val
        end if 
	End Property
	
	public Property Get Data(Key)
	    Data = Attributes("data." & Key)
	End Property

	public Property Let Data(Key,val)
	    Attributes("data." & Key) = val
	End Property
    	

    function IsInList(substring,List)
	    if isnull(List) then List=""
	    L=replace(List,", ",",")
	    IsInList=instr(1, "," & L & "," , "," & substring & ",")>0
    end function

End Class

Class EasyForm  

    private mIsFirstLoad
    private VariablesCollection

	Private Sub Class_Initialize
        set VariablesCollection = Server.CreateObject("Scripting.Dictionary")
	end sub

    Public Property Get IsFirstLoad
          IsFirstLoad = mIsFirstLoad
    End Property
    
    public sub DimVar(varName,varType)
        dim newVar 
        set newVar = new EFVariable
        newVar.init varName,varType
        VariablesCollection.Add VarName,newVar
    end sub
    
    
    sub Init()
        mIsFirstLoad = (request.form("EF_IsFirstLoad")="")
	    execute "EF_OnLoad"
        EF_HandlerName = request.form("EF_HandlerName")
        EF_HandlerArg  = request.form("EF_HandlerArg")
        if EF_HandlerName<>"" then
            if EF_HandlerArg <> "" then 
                cmd = EF_HandlerName & "(""" & EF_HandlerArg  & """)"
            else
                cmd = EF_HandlerName
            end if
            execute cmd
        end if
    end sub

    sub EndForm ()
        response.write "<input type=hidden Name=EF_IsFirstLoad value=0>"
        For each VarName in VariablesCollection.keys
            VariablesCollection(VarName).SaveState
        next    
        %>
	    <input type=hidden name=EF_HandlerName id=EF_HandlerName>
	    <input type=hidden name=EF_HandlerArg id=EF_HandlerArg>
	    
	    <input type=submit id=EF_SubmitButton style="display:none">
	    
	    <script language=javascript>
		    function EF_Call(HandlerName,arg){
                document.getElementById ("EF_HandlerName").value = HandlerName
		        if (arg!=null) document.getElementById ("EF_HandlerArg").value = arg
                document.getElementById ("EF_SubmitButton").click()
		    }
	    </script><%
    end sub
    

End Class

set EF = new EasyForm

%>
 

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Technical Lead
Italy Italy
I'm a graduate in Computer Science.
I work with Metatrader MQL4,MQL5 / C# / Asp.Net / Windows Forms / SQL Server / Access / VBA / HTML / CSS / Javascript / classic C/C++.


I also like writing songs and playing around with my band Diversamente Rossi.
This is the video of the song Un'altra estate from the album L'immobile disegno.



"Short code, good code"

Comments and Discussions