ASP dynamic web page programming experience

Source: Internet
Author: User

There are two essential conditions for learning a programming language. One is to combine theory with practice to verify the theory in books in actual routines to deepen your understanding of the theory; the second is to learn to sum up and write down the experiences and experiences in learning and application. When it is a kind of experience or lessons, it will be refined and improved in future applications, it will certainly improve your understanding of this programming language. The following are my two experiences in learning and using ASP programming. I hope to help you.

Interaction between VBScript and JScript on the ASP page

ASP is capable of managing script programs in different languages and can automatically call appropriate script engines to interpret script code and execute built-in functions. The ASP development environment provides two script engines: VBScript (default) and JScript. However, developers are not limited to using only these two languages, as long as they can provide the appropriate ActiveX Scripting Engine to use any scripting language.

Script language selection is often based on many different reasons: it may be the most familiar language for developers, it may be the most distinctive support for a given project, it may also be the most efficient. Different Environments and requirements enable us to focus on different factors when selecting a scripting language, at the same time, it also makes it difficult for us to directly provide functions inherent in other languages for the selected scripting language, or a script has been written but used in another scripting language.

What should I do now? Do you need to rewrite these scripts in the current scripting language? Or is it possible to call the built-in functions of other scripting languages in one scripting language? This article describes how to allow VBScript to interact with JScript to maximize the special support of the two scripting languages in ASP applications.

I. built-in functions of VBScript and JScript

In VBScript and JScript, a large number of built-in functions are the same or similar. However, the built-in functions in one scripting language do not always have corresponding functions in another scripting language. For example, VBScript provides many functions for operating strings and formatting data. These functions do not exist in JScript. These functions include strreverse (), filter (), and formatcurrency. On the other hand, functions provided by jscript for Array management and string encoding are not defined in VBScript, such as join (), reverse (), and pow (), bit operations, escape (), and Unescape.

So what if a VBScript function is required in a JScript program?

Ii. Mutual calls of heterogeneous scripts

If you need to call a built-in function in VBScript in a JScript script, you should write a VBScript User-Defined Function (call the built-in function in VBScript here ), then, the user-defined function is called in the JScript script just like the common JScript function.

For example, if the built-in function of VBScript to be called is formatcurrency (), you can declare the following custom function:

<Script language = "VBScript" runat = "server">
Function formatvalue (value)
Formatvalue = formatcurrency (value)
End Function
</SCRIPT>

In JScript code, you can call formatvalue () like a common JScript function. Similar methods can also be used to implement VBScript code to call the JScript function.

With the same rules, we can call any user-defined function in any script. However, you should pay attention to calling a VBScript process (sub) without parameters from a JScript script. In JScript, you should call it like calling a JScript function without parameters, for example, use Foo () to call the VBScript sub Foo process.

Iii. Data Sharing

In some cases, mixed use of VBScript and JScript functions is very useful, but sharing data between scripts in different languages may also be very useful. The sharing method is simple: No matter what language is used, variables declared at the page level can be referenced at will.

The usage of an object is similar. You can select an appropriate language to read, modify attributes, or call an object method. Of course, the attributes and methods of the given object are defined by the language in which the object instance is created. As in the preceding example, when a VBScript object without parameters is called from JScript, the calling method also complies with the calling rules of JScript, and vice versa.
Iv. array Management

Array sharing is a little more complicated. Although Arrays can be shared between scripts in different languages like other variables, you must pay attention to compatibility issues.

VBScript Arrays can be referenced by VBScript symbols in JScript, that is, using myarray (2) to reference array elements rather than the array elements of JScript to reference the symbol myarray [2]. In addition, you can use a special JScript object-vbarray object to convert a VBScript array to a JScript array. The following code creates a JScript array myjsarray from the VBScript array myvbarray:

VaR temp = new vbarray (myvbarray)
VaR myjsarray
Myjsarray = temp. toarray ()

The above code first creates a temporary vbarray object, and then uses its toarray () method to convert itself into a JScript array. Then, you can use myjsarray like a common JScript array, for example, myjsarray [1]. However, the toarray () method converts a multi-dimensional vbarray into a one-dimensional JScript array.

It is more complicated to reference the JScript array from VBScript. Although VBScript can directly access methods and attributes related to the JScript array, it does not directly access a single element of the JScript array. In other words, we can read the Length attribute of the JScript array in the VBSCRIPT script, as shown below:

X = myjsarray. Length

However, you cannot directly read a single element of the array. The following VBScript code is incorrect:

X = myjsarray (3)

A feasible solution to this problem is to execute a conversion process, as shown in the following code. It is assumed that VBScript is the default script language:

<%
Dim temp
Dim myvbarray
Temp = myjsarray. Join (",")
Myvbarray = Split (temp ,",")
%>

The jscript join () method converts the array myjsarray element to a comma-separated string. The VBScript split () function converts the string to a VBScript array. Note that the join method of JScript is called in the VBScript environment. In this example, we can use the custom VBScript function to simulate the toarray () method of the vbarray object of JScript to convert the JScript array to the VBScript array.

Create a dynamic ASP page using a template

For templates, I think everyone may have some concepts. Many templates in Word have been designed with an approximate layout. You just need to fill in those placeholder characters with your own words. This is probably the meaning of the template here. The relatively stable part of the page is fixed, and the other parts are entered with different content under different root conditions. In fact, the template function is also available in Dreamweaver, but the static one can only be filled manually, but here we talk about dynamic and Automatic Content filling.

First, let me explain why the template file is used. Sometimes, templates can bring you a more complete concept of Web Page functions and layout. When you see the template format of word, you will know what the final layout looks like, as well here. For example, you can retain ASP statements and use different templates to create different page styles.

In this way, you do not need to write different ASP pages for different types of web pages, which obviously saves us a lot of time and energy. Moreover, the template file makes it easier for you to browse Page code without worrying about the combination of ASP and HTML. You can focus only on HTML, rather than ASP. Then, you can easily understand the problem. In this article, we will use a database-a widely used employee table. Includes employee ID, name, photo, and footer of work summaries and photos. The structure of the Access database is as follows:

File Name-mydatabase. MDB
Table Name-employees
ID
Autonumber)
Fullname
Text-up to 100 characters
Picurl
Text-up to 255 characters
Duties
Annotation type
Piccaption
Text-up to 50 characters

A very simple database, right. Of course, you can expand it as required, that is, your own business. I suppose you already know the basic operations of the database, so I don't need to spend too much ink on it. In fact, the code here is very simple, as long as you know some basic things, you can understand it. After the database is created, we can create a template file. This file is the skeleton of each page. I didn't use tables, it was a little troublesome, and I was very lazy, and I didn't need to add tags, because it was to be added to ASP pages. So at the end, it looks like this:

Full name:

% Name % <br> description of duties:

% Duties %

This picture was taken: % date %

Employee ID: % empid %

That's it! This is a simple template. Save it as template. tmp and reference it on the ASP page. Note: We can add various HTML tags to the template. You can define the structure and style of the webpage in the touch version, just like actually writing a webpage, I just want to mark the key location-Have you noticed the things around %? That is the essence of the template. Perhaps you have noticed that those % circle correspond to the meaning of fields in the database. Let's take a look at the following ASP code to read the template and database files, mix them together, and then output the HTML page we want.

This is all the code. It's very simple, isn't it? All it does is open the template file, read each row in order, and then replace the % IMG % and % name % tags in the template with the actual field values read in the database. In addition, it also interprets the carriage return in a large text segment in the "Duties" field as an HTML carriage return, so as not to disrupt the overall layout format. How is it? It's easy. Templates can be used in some applications, saving time and effort. In theory, you can modify this code and use FileSystemObject to read and write the text, so that the data is not necessarily stored in the database. In some applications, such as instant news publishing, this may be more convenient.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.