20 practical examples of ASP Programming

Source: Internet
Author: User
Tags website server

1. How to use Asp to determine the virtual physical path of your website
A: Use the Mappath method.
P align = center font size = 4 face = Arial B
The Physical path to this virtual website is
B font
Font color = # FF0000 size = 6 face = Arial
% = Server. MapPath () %
Font p
2. How can I know the browser used by the user?
A: Use the Request object method.
StrBrowser = Request. ServerVariables (HTTP_USER_AGENT)
If Instr (strBrowser, MSIE) 0 Then
Response.redirect(ForMSIEOnly.htm)
Else
Response.redirect(ForAll.htm)
End If
3. How to calculate the average number of repeated visits per day
Answer: Solution
% Startdate = DateDiff (DS, Now, 01011990)
If strdate 0 then startdate = startdate-1
Avgvpd = Int (usercnt) startdate) %
Display result
% Response. write (avgvpd) %
That is it. this page have been viewed since November 10, 1998
4. How to display random images
% Dim p, ppic, dpic
Ppic = 12
Randomize
P = Int (ppicrnd) + 1)
Dpic=graphixrandompics&p&.gif
%
Display
Img src = % = dpic %
5. How to return to the previous page
A: a href = % = request. serverVariables (Http_REFERER) % preivous page
Or use an image, such:
Img src=arrowback.gif alt = % = request. serverVariables (HTTP_REFERER) %
6. How to Determine the IP address of the other party
Answer: % = Request. serverVariables (REMOTE_ADDR) %
7. How to link to an image
Answer: % @ ages = vbs quota %
% Response. expires = 0
Strimagenameappsgraphixerrorserroriamge.gif
Response. redirect (strimagename)
%
8. Force password input dialog box
A: Put this sentence at the beginning of the page.
% Response. status = 401 not Authorized
Response. end
%
9. How to pass variables from one page to another
A: Use the HIDDEN type to pass variables.
% Form method = post action = mynextpage. asp
% For each item in request. form %
Input namee = % = item % type = HIDDEN
Value = % = server. HTMLEncode (Request. form (item) %
% Next %
Form
10. Why do I use msgbox in an asp program? If a program error occurs, I have no permission.
A: Because asp is run on the server, if you can display a dialog box on the server, you have to wait for someone to confirm before your program can continue to run, in general, the server won't be guarded, so Microsoft has to disable this function and tell you that you have no permissions. However, the combination of ASP and client scripts can display a dialog box, as shown below:
% YourVar = test dialog box %
% S required language = ''javas upload''
Alert (% = yourvar %)
S Branch
11. Is there any way to protect your source code?
A: You can download a Microsoft Windows s javasencoder, which can encrypt asp scripts and client javas criptvbs keystore scripts.
12. How can I transmit a query string from one asp file to another?
A: Add the following statement to the former file:
Response. Redirect (second. asp & Request. ServerVariables (QUERY_STRING ))
13. The global. asa file always does not work.
A: only when the web directory is set to web application and global. asa is valid, and global. asa is valid under the root directory of a web application. How can IIS use Internet Service Manager to set application setting so that htm files can execute script code like asp files?
14. How can I make the htm file execute script code like an asp file?
Answer: Internet Sevices Manager-select default web site-right mouse key-menu Attribute-> Home Directory-Application Setting) -Click "configuration"-"app mapping"-"Add-executable browse" and select "WINNTSYSTEM32INETSRVASP. dll extension input htm method exclusions input PUT. DELETE all. However, it is worth noting that the htm should also be processed by asp. dll, reducing the efficiency.
15. How to register Components
A: There are two methods.
Method 1: manually register DLL. This method is always used by IIs and other Web servers. It needs to be executed in the command line mode, enter the directory containing DLL, and enter: regsvr32 component_name.dll such as ctempregsvr32 AspEmail. dll, which registers the specific dll information into the registry on the server. Then this component can be used on the server, but this method has a defect. After the component is registered using this method, the component must have the permission to execute the dll by setting the NT anonymous account. In particular, some components need to read the Registry. Therefore, the method of registering this component is to cancel the registration of this dll only when there is no MTS on the server, and use: regsvr32 u aspobject. dll example ctempregsvr32 u aneiodbc. dll
Method 2: using MTS (Microsoft Transaction Server) MTS allows you to specify that only privileged users can access components, greatly improving the security settings on the website Server. To register a component on MTS, follow these steps:
1) Open the IIS console.
2) Expand transaction server, right-click pkgs installed, and select new package.
3) Click create an empty package.
4) Name the package.
5) Specify the administrator account or use interactive (if the server often uses administrator login ).
6) Right-click the expanded components under the package you just created. Select new then component.
7) Select install new component [B].
8) Find Your. dll file and select next.
To delete this object, select its icon and then delete.
Note: pay special attention to the second method, which is the best way to debug your own component, instead of restarting the machine every time.

16. ASP and Access database connection:
<% @ Language = VBs certificate %>
<%
Dim conn, mdbfile
Mdbfile = server. mappath ("database name. mdb ")
Set conn = server. createobject ("adodb. connection ")
Conn. open "driver = {microsoft access driver (*. mdb)}; uid = admin; pwd = Database Password; dbq =" & mdbfile
%>
17. ASP and SQL database connection:
<% @ Language = VBs certificate %>
<%
Dim conn
Set conn = server. createobject ("ADODB. connection ")
Con. open "PROVIDER = SQLOLEDB; data source = SQL server name or IP address; UID = sa; PWD = DATABASE Password; DATABASE = DATABASE Name
%>
Create a record set object:
Set rs = server. createobject ("adodb. recordset ")
Rs. open SQL statement, conn, 3, 2
18. Common SQL commands:
(1) Data Record Filtering:
SQL = "select * from data table where field name = Field Value order by field name [desc]"
SQL = "select * from data table where field name like'' % Field Value % ''order by field name [desc]"
SQL = "select top 10 * from data table where field name order by field name [desc]"
SQL = "select * from data table where field name in ('' value 1 '','' value 2 '','' value 3 '')"
SQL = "select * from data table where field name between value 1 and value 2"
(2) update data records:
SQL = "update data table set field name = field value where condition expression"
SQL = "update data table set field 1 = value 1, Field 2 = value 2 ...... Field n = value n where condition expression"
(3) Delete data records:
SQL = "delete from data table where condition expression"
SQL = "delete from data table" (delete all data table Records)
(4) add data records:
SQL = "insert into data table (Field 1, Field 2, Field 3 ...) Valuess (value 1, value 2, value 3 ...) "
SQL = "insert into target data table select * from source data table" (add records of source data table to target data table)
(5) statistical functions of data records:
AVG (field name) returns the average value of a table column
COUNT (* | field name) statistics on the number of data rows or the number of data rows with values in a column
MAX (field name) obtains the maximum value of a table column.
MIN (field name) obtains the minimum value of a table column.
SUM (field name) adds the values in the data column
The method for referencing the above functions:
SQL = "select sum (field name) as Alias from data table where condition expression"
Set rs = conn. excute (SQL)
Use rs ("alias") to obtain the calculation value. Use the same method for other functions.
Create and delete data tables:
Create table data TABLE name (Field 1 type 1 (length), Field 2 type 2 (length )...... )
Example:
Create table tab01 (name varchar (50), datetime default now ())
Drop table data TABLE name
(Permanently delete a data table)
19. Method of record set object:
Rs. movenext moves the record pointer down a row from the current position
Rs. moveprevious transfers the record pointer from the current position to a row up
Rs. movefirst move the record pointer to the first row of the data table
Rs. movelast moves the record pointer to the last row of the data table
Rs. absoluteposition = N move the record pointer to the nth row of the data table
Rs. absolutepage = N move the record pointer to the first row of page N
Rs. pagesize = N set N records per page
Rs. pagecount the total number of pages returned Based on pagesize settings
Rs. recordcount total number of returned records
Rs. bof indicates whether the record pointer exceeds the first end of the data table. true indicates yes, and false indicates no.
Rs. eof indicates whether the returned record pointer exceeds the end Of the data table. true indicates yes, and false indicates no.
Rs. delete deletes the current record, but the record pointer does not move down
Rs. addnew add record to end of data table
Rs. update data table records
20 Recordset object Method
Open Method
Recordset. Open Source, ActiveConnection, CursorType, LockType, Options
Source
The Recordset object can be connected to the Command object through the Source attribute. The Source parameter can be a Command object name, an SQL Command, a specified data table name, or a Stored Procedure. If this parameter is omitted, the system uses the Source attribute of the Recordset object.
ActiveConnection
The Recordset object can be connected to the Connection object through the ActiveConnection attribute. ActiveConnection can be a Connection object or a string containing the database Connection information (ConnectionString.
CursorType
The CursorType parameter of the Open method of the Recordset object indicates the type of cursor to start data, including adOpenForwardOnly, adOpenKeyset, adOpenDynamic, and adOpenStatic, which are described as follows:
Constant Value description
AdOpenForwardOnly 0 default value, start a Forward-Only cursor (Forward Only)
AdOpenKeyset 1 starts a Keyset type cursor
AdOpenDynamic 2 starts a Dynamic type cursor
AdOpenStatic 3 starts a Static cursor

The preceding cursor types directly affect all attributes and methods of the Recordset object. The following lists the differences between them.
Recordset attributes
The NextRecordset method is not applicable to Microsoft Access databases.
LockType
The LockType parameter of the Open method of the Recordset object indicates the Lock type to be used. If this parameter is ignored, the system uses the LockType attribute of the Recordset object as the default value. The LockType parameters include adLockReadOnly, adLockPrssimistic, adLockOptimistic, and adLockBatchOptimistic, which are described as follows:
Constant Value description
AdLockReadOnly 1 is the default value. The Recordset object is started in read-only mode and cannot run AddNew, Update, Delete, and other methods.
AdLockPrssimistic 2 when the data source is being updated, the system will temporarily lock the actions of other users to maintain data consistency
AdLockOptimistic 3 when the data source is being updated, the system does not lock the actions of other users. Other users can add, delete, and modify data.
AdLockBatchOptimistic 4 when the data source is being updated, other users must change the CursorLocation attribute to adUdeClientBatch to add, delete, and modify the data.

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.