Example of ASP and TXT combination design program

Source: Internet
Author: User
Tags format count end functions readline string access
Program | design

Introduction: ASP is currently the most widely used on the internet, one of the dynamic Web programming language. And the mention of it we are always with access or databases such as sqlsever together, it is true that their combination can make us more free and easy to manage and publish the site, but you can know that ASP and text files can be combined to achieve some small and practical functions oh. Now, with three of small examples that apply to Web pages, let's take a look at the combination of the two.

ASP is one of the most widely used dynamic Web page programming languages on the Internet at present. And the mention of it we are always with access or databases such as sqlsever together, it is true that their combination can make us more free and easy to manage and publish the site, but you can know that ASP and text files can be combined to achieve some small and practical functions oh. Now, with three of small examples that apply to Web pages, let's take a look at the combination of the two.

In the ASP we can use the FileSystemObject object's OpenTextFile method to open the text file in the following format:

Set fso=server.createobject ("Scripting.FileSystemObject")
Set Fp=fso. OpenTextFile (Server.MapPath ("file.txt")) ' file.txt is the text file you want to open

Knowing this, we first write a simple "message publishing system". Create a Message.txt file by referencing the format of Figure 1 first.

Then run any text editor to input the following code, save it in the same directory as Message.txt, named Message.asp.

<body>
<table border= "1" cellspacing= "0" cellpadding= "1" bordercolorlight= "#dfdfdf" bordercolordark= "#ffffff" align= " Center ">
<tr bgcolor= "#cccce6" >
<TD algin= "center" colspan= "2" > Latest news </td>
</tr>
<%
Set fso=server.createobject ("Scripting.FileSystemObject")
Set Fp=fso. OpenTextFile (Server.MapPath ("Message.txt"))
' Open Message.txt

Do while FP. Atendofstream<>true
S=FP. ReadLine ' reads a piece of data

P=instr (S, "*") ' Get the position of the #
Messagetitle=mid (s,1,p-1) ' Gets the string (message header) at the front of the * number
S=mid (S,p+1,len (s)-P) ' Set the variable s to the string behind the * number

P=instr (S, "*")
Messageurl=mid (s,1,p-1) ' Obtain the string (link address) behind the * number
S=mid (S,p+1,len (s)-P)

Ttime=s ' Set message release time Ttime to S
%>
<tr>

<td><a href= ' <%=MessageUrl%> ' ><%=Messagetitle%></a></td>
<td><%=ttime%></td>
</tr>
<!--generate the form and display the message-->
<%
Loop
Fp.close
Set fp=nothing
Set fso=nothing
%>
</table>
</body>

Done! Run and see if the results are the same as Figure 2?

The principle is to use ASP to open the Message.txt, and then the * as the demarcation point to get the message title, link address and release time, and then generate a table to display the information obtained, and then loop read Message.txt the next line of information and generate a table. We can see that if you want to update the message, just change the contents of the text file, so update although upload download slightly trouble, but its easy to write, fast, occupy resources and so on the merits of the small and so is worth a try. Especially for free space abroad (those spaces only support ASP and do not support databases).

After completing the first example, let's make a counter. Similarly, it also records data (current traffic) in a TXT file. Let's take a look at the program code:

<%
Set Rs=server.createobject ("Scripting.FileSystemObject")
File=server.mappath ("Count.txt") ' establishes a link with count.txt
Set Cn=rs.opentextfile (file,1)
Count=cn.readline ' Get the fields in the text file (access amount)
If session (' in ') <> ' in ' Then
Session (' in ') = ' in '
Set Cn=rs.createtextfile (file,true)
Application.Lock
Count=cint (count) +1 ' accesses the traffic plus 1 to get the current traffic
Cn.writeline Count ' writes the current access amount to Count.txt
Application.UnLock
End If
%>
<%=count%> ' output

Then you need to create a text file called count, and directly enter a number in it, such as "0." So this 0 is the starting value of the counter, from the code we can know that the program will automatically add this number, and in the course of use, we can change this number to modify the site's current traffic (directly modified to 1000000?). Khan). That's right, also to say that these two files are saved in the same directory.

Finally, we will use the ASP built-in a component Ad rotator to write a banner management system to manage the banner on the site. It allows you to randomly display all the banner you need to display in accordance with the predetermined odds.

First, create an ASP file called AR, which reads as follows:
<%
Set ar=server.createobject ("MSWC. AdRotator ")" creates a AdRotator instance called AR
Response.Write ar. GetAdvertisement ("Ad.txt") reads related content in Ad.txt
%>
' The program completes and then creates the adgo.asp, which reads as follows:
<%
Response.Redirect (Request.QueryString ("URL"))
%>

This program has only one sentence. First, use the QueryString of the request object to obtain the URL, and then use the Response object's Redirect method to access the link.

When the program is done, open your Notepad and enter the following:

Redirect adgo.asp
Width 83
Height 31
Border 0
*
Http://www.pcdiy.com.cn/banner.gif
http://www.pcdiy.com.cn/
NET friend World, savor e life
20
Http://game.sina.com.cn/banner.gif
http://game.sina.com.cn/
Enter Sina game world
20
Http://www.google.com/banner.gif
http://www.google.com/
Do you want to search?
10

Save it as Ad.txt, and remember to save the three files in the same directory. The first line of the ad.txt specifies that the mouse clicks banner to the adgo.asp. The three lines below define the size and border thickness of the banner. "*" indicates the end of the configuration information. Then down is the banner information. Each banner is defined by four defining information, which in turn defines the location of the banner, the URL address of the link, the description and display ratio (the greater the number of times the value appears). In this below you can add n banner information, but remember to 4 lines of a group, do not get wrong.

With these examples, you have a taste of the power of the asp+txt combination, and these three examples are the simplest applications that you can use to improve the functionality that enhances them. Extrapolate you can also develop a good ASP program to. Finally, I wish your site more and more wonderful!



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.