ASP Class entry recommended _asp class

Source: Internet
Author: User
Tags vbscript class

Class Declaration

Declaring the name of a class means defining variables, attributes, and methods to form a class. We often see other programming languages have classes in the description, php,vb,c++, this in VBScript class description, I was the first time to hear, our daily work is the website development, in this more or less to make some experience, can also boast as "expert", So I'm going to share what I know about this new stuff. Let's take a look at the code below! (window2000+iis5.0 passed the test)

Definition of Class 1

Yyh.asp
<%
"Declares a class named YH
Class YH

Private YH

Initialization of the ' class '
Private Sub Class_Initialize
Yh= "Tianya"
End Sub

"Define a function
Public Function yyh (a,b)
Yyh=a+b
End Function

"Define a method
Public Sub Yyh1 (STAT)
Response.Write Stat
End Sub

End Class

Set myyyh=new YH ' defines an instance of a Myyyh object named YH
Response.Write Myyyh.yyh (6,6) & "<br>"
Mystring= "This is the way of the Tianya
MYYYH.YYH1 mystring

%>

This is a very simple program, in which we declare a class named YH, set up a yyh function, a yyh1 method, which is very simple to believe that you can read, it shows as follows:

12
This is the way of the Tianya

We can write a class of the program we use, and then we'll use <! --#include file= "yyh.asp"--> to include in the line, which gives us a new space for the development of the program.


Definition of Class 2

This uses the properties of the class to define the method.

<%
"Declares a class named MyClass
Class MyClass

Private A1,B1

Initialization of the ' class '
Private Sub Class_Initialize
A1=0
B1=0
End Sub

' Defines a property
Public Property Let width (ax)
A1=ax
End Property

' Define another attribute
Public Property Let height (BX)
B1=bx
End Property

"Calculates the result of two property values and gets a new property
Public Property Get Area
Area=b1*a1
End Property

End Class

Set tianya=new MyClass ' defines an instance of an object MyClass named Tianya
Tianya.width=50
Tianya.height=60
Response.Write Tianya.area
%>


An ASP class model of complete database management

"The end of the clouds original

First build a database user, there is a table named user,
There are three fields in the table, respectively, id,name,content

To write the database connection file first:
' Conn.asp

<%
Strsql= "dbq=" +server.mappath ("User.mdb") + ";D river={microsoft Access DRIVER (*.mdb)};"
Set conn=server.createobject ("ADODB. CONNECTION ")
Conn.Open strSQL
%>

To construct the UserClass class:
, mycls.asp
<%
Class UserClass
Private id,name,content

Initialization of Private Sub Class_Initialize () class, connecting the database
Username= ""
Usercontent= ""
End Sub

' Several properties of the following settings class
Public Property Let UserID (Vnewvalue)
Id=cint (Vnewvalue)
End Property

Public Property Get UserID
Userid=id
End Property


Public Property Let username (Vnewvalue)
Name=vnewvalue
End Property

Public Property Get username
Username=name
End Property

Public Property Let Usercontent (Vnewvalue)
Content=vnewvalue
End Property

Public Property Get Usercontent
Usercontent=content
End Property

' Add a record
Public Sub AddUser ()
If username <> "" and Usercontent <> "" Then
Set rs = Server.CreateObject ("ADODB.") Recordset ")
Sql= "Select * from User"
Rs. Open sql,conn,1,3
Rs. AddNew
RS ("name") = Username
RS ("Content") = Usercontent
Rs. Update
Rs. Close
Set rs = Nothing
Response.Write "Add record success!"
End If
End Sub


' Display a record
Public Sub Showuser ()
Set Rs=server.createobject ("Adodb.recordset")
Sql= "SELECT * from user where id=" & userid
Rs.Open sql,conn,1,3
Username=rs ("name")
Usercontent=rs ("content")
Rs.close
End Sub

' Edit record
Public Sub Edit ()
Set Rs=server.createobject ("Adodb.recordset")
Sql= "SELECT * from user where id=" & userid
Rs.Open sql,conn,1,1
Username=rs ("name")
Usercontent=rs ("content")
Rs.close
End Sub

' Save edit
Public Sub Saveedit ()
Set Rs=server.createobject ("Adodb.recordset")
Sql= "SELECT * from user where id =" & userid
Rs.Open sql,conn,1,3
RS ("name") =username
RS ("content") =usercontent
Rs.update
Rs.close
Response.Write "Update record successful!"
End Sub

' Delete Record
Public Sub Deluser ()
Set Rs=server.createobject ("Adodb.recordset")
Sql= "Delete from user where id=" & userid
Rs.Open sql,conn,1,1
Set rs=nothing
Response.Write "Delete Record successful!"
End Sub

' Challenges paging display ~~!!
Public Sub List (n)
Dim page
Page=request ("page")
PageSize = n
Dim rs,strsql,news
strSQL = "SELECT * from user order by ID DESC"

Set rs = Server.CreateObject ("ADODB.") Recordset ")
Rs.Open strsql,conn,3,3
Rs. PageSize = PageSize
Totalfilm=rs.recordcount
Pgnum=rs. PageCount
If page= "" or CLng (page) <1 then page=1
If CLng (page) > Pgnum then Page=pgnum
If Pgnum>0 then Rs. Absolutepage=page

If Rs.eof then
Response.Write "<font color= ' #003366 ' class= ' 3dfont ' > No record!</font> '
Else
Count=0
Do but not (rs.eof or RS.BOF) and Count<rs. PageSize
with response
. Write "<table><tr><td>"
.write  RS ("id") & ""
. Write "<a href=show.asp?id=" &rs ("id") & ">"
. Write rs ("name")
. Write "</a> "
. Write "content: "&rs (" content ")
. Write" </td></tr></table> "
End with
Rs.movenext
Count=count+1
Loop
End If
with response
. Write <table><tr><td> total <b>
. Write Rs.pagecount
. Write </b> page
for I=1 to Rs.pagecount
. Write "<a href=list.asp?page=" &i& ">"
. Write I
. Write "</a>"
Next
Rs.close
set rs=nothing
. Write "</td></tr></table>"
End with
End Sub


' Class exit after doing cleanup work
Private Sub Class_Terminate ()
If IsObject (Conn) Then Conn.Close:Set Conn = Nothing
End Sub
End Class
%>
(The above procedure is passed in Winxpsp2+netbox)


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.