Three techniques of ASP development

Source: Internet
Author: User
Tags constant contains execution implement include return uuid
Tip One, server-side file dynamic contains a note
During ASP development, we often make common functions into a separate ASP file, and then introduce the required pages through the Include method. Because during execution, the server first runs the file included with the Include method and then the code for the current page, it is not possible to use the Include method to implement the dynamic inclusion on the server side during the actual execution process.
Look at the following code:
<%
I=1
str = "File" & I & "/a.asp"
%>
<!--#include virtual = "<% str%>"-->
Our intention is to have different values based on I, including files of the same name under different directories, but in the course of execution, they will return "cannot find the containing file ' <% str%> '." "The mistake. That is, in the ASP, we cannot use the Include method to implement the dynamic inclusion of the file.
To implement real dynamic file inclusion in ASP, it is now possible to use the Server.Execute command. Server.Execute is a new approach to the ASP3.0 of new program flow, which can be temporarily transferred to a file, execute its contents, and then return to the original program during the run.
The code that is written back with the Server.Execute command is as follows:
<%
I=1
str = "File" & I & "/a.asp"
Server.Execute Str
%>
Note: This article discusses the dynamic inclusion of the file, mainly refers to the case containing the file as a variable, different from the included file name in advance to determine the situation.

Ii. Bulk deletion of database records
In ASP development management programs, it will inevitably involve the operation of deleting database records. Generally we will delete records in the following two ways: Using the Recordset.delete method or directly using the SQL statement "Delete from data table Where PRIMARY key = value". The method that this article will introduce can delete one or more records at the same time. Let's start with a concrete example where we'll use a data table (Subject) and two ASP files (list.asp and code.asp) that contain two fields:
ID, data table primary key, used to uniquely Mark records in the database;
Title: the caption used to display the contents of a record.
The contents of the two ASP files are as follows:
List.asp
<%@ Language=vbscript%>
<%

' Create a database connection
Dim objconn
Set objconn = Server.CreateObject ("ADODB. Connection ")
objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\samples\tooltips.mdb"

' Create a recordset
Dim objRS
Set objRS = Server.CreateObject ("ADODB. Recordset ")
Objrs.open "Subject", objconn

' Show list of records
Response.Write "<form method=post action=" "Code.asp" ">"
Response.Write "<table border=1 cellspacing=1>"

Response.Write "<TR>"
Response.Write "<TH> Delete </TH>"
Response.Write "<TH> title </TH>"
Response.Write "</TR>"

Do as Not objrs.eof
Response.Write "<TR>"
Response.Write "<td><input Type=checkbox name=delete"
Response.Write "Value=" & CInt (objRS ("ID")) & "></TD>"
Response.Write "<TD>" & objRS ("Title") & "</TD>"
Response.Write "</TD></TR>"
Objrs.movenext
Loop

Response.Write "</TABLE>"
Response.Write "<p><input type=submit value=" "Delete" ">"
Response.Write "</FORM>"

' Close ADO Object
Objrs.close
Set objRS = Nothing

Objconn.close
Set objconn = Nothing
%>
Code.asp
<%@ Language=vbscript%>
<%

' Get list of deleted records
Dim strdeletelist
Strdeletelist = Request.Form ("Delete")


' Create a Connection object
Dim objconn
Set objconn = Server.CreateObject ("ADODB. Connection ")
objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\samples\tooltips.mdb"

' Generate the SQL statement that deletes the record
Dim strSQL
strSQL = "DELETE from Subject WHERE ID" ("& Strdeletelist &")

' Perform the delete operation
objConn.Execute strSQL

' Close the Connection object
Objconn.close
Set objconn = Nothing
%>
As you can see from the code above, We first generate a list of records in the List.asp page, each record is preceded by a checkbox that uses the same name, and each checkbox's value corresponds to a unique record in the datasheet, because in ASP, if a form contains multiple identically named controls, it returns A string separated by "," so, in the Code.asp file, we first get the value of all the checkbox, and then complete the deletion of the record with an SQL statement "delete from subject where ID" (str). The key is the SQL statement "DELETE from Tablenamewhere TableID in (String)." Because the SQL statement has no special requirements for string, we can either delete a record or delete more than one record at a time.
It's also important to note that if the field type used in the datasheet is a character type, it needs to be modified in the following form
......
Strdeletelist = Replace (Strdeletelist, ",", "', '")
strSQL = "DELETE from Subject WHERE ID" (' & Strdeletelist & ')
......

Third, the use of metadata tag contains external constants
If we want to use ADO predefined constants in an ASP program, you must include "adovbs.inc" on each page that uses an ADO constant, otherwise you can only represent them directly with numbers. Clearly, direct numbers are not conducive to future changes in the program, and can be standardized to develop the need to avoid things, but each page contains "Adovbs.inc", but also a bit cumbersome. So, here's another way to include this one time. This is the TypeLibrary Declarations in Global.asa.
Because generic COM objects include constant definitions in the type library, you can use typelibrary declarations to directly read the defined constants in these type libraries. Plus typelibrary declarations can be used in all ASP pages as long as it is defined once in Global.asa, and is significantly more efficient than a single page contains.
The grammatical form of typelibrary declarations is:
<!--METADATA
Type= "TypeLib"
file= "File"
Uuid= "Typelibraryuuid"
-->
Where the file and uuid two properties are optional. The file property refers to the full file path that contains the definition of the constant, whereas the UUID refers to the GUID of the external library.
For example, if we include an ADO constant in an ASP program, we can use the following two ways:
<!-metadata
Type = "TypeLib"
File = "C:\Program Files\Common Files\system\ado\msado21.tlb"
-->
Or
<!-metadata
Type = "TypeLib"
UUID = "00000201-0000-0010-8000-00AA006D2EA4"
-->
I believe that we have seen the above two methods, it will be natural to choose the first. It is true that using the physical directory is much easier than the latter, but if you are developing an ASP program that needs to be installed on another machine, the second approach is better. For your convenience, here are some commonly used GUIDs in ASP
Name
Guid
ADO2.1
{00000201-0000-0010-8000-00AA006D2EA4}
ADO2.5
{00000205-0000-0010-8000-00AA006D2EA4}
ADO2.6
{00000206-0000-0010-8000-00AA006D2EA4}
ADO2.7
{00000300-0000-0010-8000-00AA006D2EA4}
FileSystemObject
{420b2830-e718-11cf-893d-00a0c9054228}

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.