Allow me to introduce myself. My name is Rob Collyer, I lay eyes upon ASP years ago and have been heavily coding since. It always struck me as a complete pain in the neck the way in which we have all been dealing with variables in ASP, for as long as we have used ASP.What do I mean? ........ Allow me to explain with this example of the hard way:-
<!% FirstName = Request.Form("FirstName") Surname = Request.Form("Surname") Address1 = Request.Form("Address1") ...Allow me to introduce myself.
My name is Rob Collyer, I lay eyes upon ASP years ago and have been heavily coding since. It always struck me as a complete pain in the neck the way in which we have all been dealing with variables in ASP, for as long as we have used ASP.
What do I mean? ........ Allow me to explain with this example of the hard way:- [code] <% FirstName = Request.Form("FirstName") Surname = Request.Form("Surname") Address1 = Request.Form("Address1") Address2 = Request.Fomr(Address2") ... ... %>
Rather than go on longer than necessary into yet more lines looking exactly the same as the ones above, to put my point across, I'll put you out of your misery.Why do variables have to be this TEDIOUS to deal with??? I wanted a way to read in all variables at once in one go, with just one function call.... I must be mad.... sure enough, I was told this as well as things like 'It's not possible', etc.Sure enough, at the time, it wasn't possible. To my rescue came Microsoft, with Version 5 of the VBScript engine and a new command called Execute. Execute(string) basically executes a string as though it were ASP.. So Execute("Response.write ""Hello"") .... would have the same effect as just using response.write "hello"As soon as I saw this new feature I knew instantly what I could do with it:-
<% For Each Field in Request.Form TheString = Field & "= Request.Form(""" & Field & """)" Execute(TheString) Next %>
We are setting variables automatically. That one little tiny piece of code, has saved me so much time over the last year, you will not believe.Ok, it does have it flaws, Forms with image submit buttons pass form variable names like "submit.X", and "submit.Y" (Co-ordinates where you clicked on the submit image) which obviously wont do for variable names in ASP, and results in the white error page from hell.... But we can start to prevent these things:-
<% For Each Field in Request.Form tmpField = Replace(Field,".","") 'remove full stops TheString = tmpField & "= Request.Form(""" & Field & """)" Execute(TheString) Next %>
Why stop there?? There are many silly characters that are fine in HTML controls as names, but when setting variable names automatically like this in VBScript, these characters will not do. You can easily add additional lines as above to escape other characters like spaces, hyphens and all manner of other ones too.You've got multiple controls on forms.... how are you gonna assign variables to multiple select boxes?As a developer, I found it handy to read these 'multiple' part form variables into an array... I will build upon the above code to handle multiple items and create arrays from them:-
<% For Each Field in Request.Form tmpField = Replace(Field,".","") 'remove full stops ItemCount=Request.Form(Field).count IF ItemCount > 1 then execute "redim " & field & "(" & itemcount -1 & ")" 'Dynamically dimension the array For Item = 0 To ItemCount - 1 TheString = tmpField & "("&Item&")= request.form(""" & field & """).item(" & Item + 1& ")" Next Else TheString = tmpField & "= Request.Form(""" & Field & """)" End If Execute(TheString) Next %>
I hope by now you can see the potential of what all of the above means to you as the developer.....I urge you all to write your own functions that'll handle variables the way you want them to, and please remember.... FORMS were just an example, don't stop there, after all there are Querystrings, Cookies, Session variables, Application Variables, ServerVariables. Then there are recordsets (why not?) and the dictionary objects, etc, etc, etc.Just remember, you are now armed with info which will save you a great deal of time, why not get busy writing some variable handling functions of your own??If anybody wants a function to selectively handle Form, Querystring, Cookies, Session, Application and Server variables then I have one...just mail me. It also has a parameter to run in DEBUG mode which will show you variables in all objects, their values and whether their names present a problem during conversion. It'll also flag those controls that can be set to arrays, etc.Anyway, those that do feel inspired to get writing their own handlers, let me ask one thing.... Send me a copy of your finished function(s), this is pretty new stuff and I'm well open to being inspired by YOUR code.Feel free to contact me with your feedback.Enjoy!Rob CollyerASPwiz@hotmail.com | DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP Articles More By Robert Collyer developerWorks - FREE Tools! | WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
| | | | Learn from the best! Find out how developers use Rational ClearCase to be more flexible, innovative and deliver higher quality code in the Rational ClearCase Power Users eKit. This complimentary eKit provides a collection of materials, like articles, whitepapers, and demos that can help you become a power user of Rational ClearCase. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement. FREE! Go There Now!
| | | | Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. FREE! Go There Now!
| | | | Get a free trial download of IBM Lotus Forms V3.0 (formerly Workplace Forms), which provides a zero-footprint eForms solution to help you automate and move forms-based business processes off the desktop and onto the Web. With Lotus Forms, you can extend applications beyond the firewall by creating a single electronic form document ready for use in both thick and Web 2.0 thin client format. FREE! Go There Now!
| | | | In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
| | | | As organizations integrate software into every aspect of business, they are constantly pressured to deliver faster, better, and cheaper results. Unfortunately, a “dis-integrated” software delivery approach reduces returns while increasing costs. This IBM Rational White Paper shows how Integrated Requirements Management aligns organizations around maximizing value and keeping pace with change. FREE! Go There Now!
| | | | User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges. FREE! Go There Now!
| | | | Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications. FREE! Go There Now!
| | | | Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |