Introduction to ASP Class

Source: Internet
Author: User

Class Declaration

Declaring a class name is to define some variables, attributes, and methods to form a class. We often see descriptions of classes in other programming languages, such as PHP, VB, and C ++. This class description in VBScript is the first time I have heard of it, our daily work is website development. In this case, we have more or less experience, and we can also boast of being "professional". So I will share what I know about this new stuff. Let's take a look at the following code! (Window2000 + IIS5.0 passed the test)

Class Definition 1

Yyh. asp
<%
''Declares a class named yh.
Class yh

Private yh

''Class initialization
Private Sub Class_Initialize
Yh = "Tianya Fengyun"
End Sub

''Defines a function
Public Function yyh (a, B)
Yyh = a + B
End Function

''Defines a method
Public sub yyh1 (stat)
Response. write stat
End Sub

End Class

Set myyyh = New yh'' defines a myyyh object instance named yh.
Response. write myyyh. yyh (6, 6) & "<br>"
Mystring = "this is the method of the world"
Myyyh. yyh1 mystring

%>

This is a very simple program. We declare a class named yh in it, and establish a yyh function and a yyh1 method. This program is very simple and can be understood by everyone. Its display is as follows:

12
This is the method of the world

You can write the commonly used programs into a class, and then use it <! -- # Include file = "yyh. asp" --> to include the file, which provides a new space for the development program.

Class Definition 2

The class attribute definition method is used here.

<%
''Declares a class named myclass.
Class myclass

Private a1, b1

''Class initialization
Private Sub Class_Initialize
A1 = 0
B1 = 0
End Sub

''Defines an attribute
Public Property Let width (ax)
A1 = ax
End Property

''Defines another property
Public Property Let height (bx)
B1 = bx
End Property

''Calculates the result of two attribute values to obtain a new attribute.
Public Property Get area
Area = b1 * a1
End Property

End Class

Set tianya = New myclass ''defines an instance of the object myclass named tianya
Tianya. width = 50
Tianya. height = 60
Response. write tianya. area
%>

A fully-managed asp model

'Tianya Fengyun originality

First, create a database user with a table named user,
The table contains three fields: id, name, and content.

Write the database connection file first:
'Conn. asp

<%
StrSQL = "DBQ =" + server. mappath ("user. mdb") + "; DRIVER = {Microsoft Access Driver (*. mdb )};"
Set conn = server. createobject ("ADODB. CONNECTION ")
Conn. open StrSQL
%>

Construct the userclass class:
, Mycls. asp
<%
Class userclass
Private id, name, content

Private Sub Class_Initialize () 'class initialization, connection to the database
Username = ""
Usercontent = ""
End sub

'Set the attributes of the class below
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 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 "record added successfully! "
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 and 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 "the record is deleted successfully! "
End sub

'Challenge page 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 while 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
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
End sub

'Class exited for cleanup
Private Sub class_terminate ()
If IsObject (Conn) Then Conn. Close: Set Conn = Nothing
End Sub
End Class
%>
(The above program 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.