Recommended: Use ASP to make pie charts, bar charts, etc. In our work, we often need to convert data into bar charts, pie charts, etc. to facilitate and intuitive analysis of data. Here I will introduce to you a component that makes pie charts and bar charts in ASP: csDrawGraph, csdgt.zip. Because it is a component, we need to use REGSV before using it.
Object-oriented programming
As the complexity of program design increases, structured programming methods are not enough. The fundamental reason for insufficient use is that it is inconvenient to reuse the code. An object-oriented method was born, and it implements relatively complete code reuse functions through inheritance. Many students are often asked a question during interviews when applying for jobs. Let’s talk about what object-oriented programming is. The students are speechless and come back to ask me how to answer this question. I told him that just one word is enough. Object-oriented programming is the encapsulation of data; programming of paradigms (templates) is the encapsulation of algorithms. Later, when a student encountered this problem, he answered it simply with a simple sentence, and the other party looked at the student with admiration (the student told me proudly later). Why that? Because only through thorough experience and practice can this essence be extracted.
Object-oriented design methods and ideas were actually proposed as early as the early 1970s. The purpose is to force the program to manipulate data through functions. This way, data encapsulation is implemented, and the bug caused by any code in the previous design method can be avoided. It is very difficult to find and modify this bug. Then you can say that even if I don't use object-oriented, when I want to access a certain data, I just use the call function to access it, wouldn't it be fine? Yes, it is OK, but it is not mandatory. People are lazy. When I want to add 1 to i, why do I have to call a function? Forget it, it's easier to save trouble. Haha, because of this laziness, it is difficult to catch when the program has a bug. Object-oriented is mandatory, and it solves your laziness problem from the compilation stage.
Coincidentally, object-oriented thinking is actually consistent with dealing with problems in our daily lives. For example, I plan to throw away a teacup, how can I throw it away? It's too simple, pick up the teacup, walk to the trash can, and throw it away! Pay attention to analyzing this process. We first select an object--------------------------------------------------------------------------------------------------------------- The actions each object can apply to it are limited: a teacup can be thrown, smashed, used to drink water, knock it to make sounds...; a piece of paper can be written, tear, burned... That is to say, once an object is determined, the method is determined as well. This is how our daily life is. However, when you think about our programming and computer operations, this is not the case. Take the DOS operation as an example. I want to delete a file by: c:> del file name <Enter> at the DOS prompt. Pay attention to this process. The actions are in front (del) and the objects are in the back (file name), and the object-oriented methods are in the opposite order. So it's just a matter of order, what impact will it have? Haha, you must have seen this phenomenon: File not found. Ah~~~, I was wrong, I was wrong, I typed a letter in the wrong file name, so I re-enter: c:> del File name 2 <Enter>. Unfortunately happens again, computer report: File read only. Haha, pain :). So the operation of DOS actually violates our daily habits (of course, no one has raised any objections before), but now because of the use of object-oriented design, these problems are solved at compile time, not at runtime. obj.fun(), for this statement, whether it is an object or a function, if there is any problem with your input, it will be reported during compilation, so that you can modify it, rather than making errors during execution, which will cause you to catch bugs everywhere.
At the same time, object-oriented can solve the problem of code reuse - inheritance. I used to write a dog class, with attributes (variables): hairy, 4 legs, a raised tail (the one with a drooping tail is a wolf), a very sensitive nose, and likes to eat meat and bones... The methods include (function): able to run, smell, barking... If it catches rats, people will call it meddling. OK, the dog type has been written. But in my actual life, the dog I have is very similar to the dog I wrote before, with only a little difference, that is, my dog, it is: curly and long, with a small nose, a small mouth... So, I derived a new type called Pug and added new features to the dog. OK, the program is finished and the correct code from the previous one is reused - this is the benefit of object-oriented programming. My success is just standing on the shoulders of the giant. Of course, if you use VC, the code that reuses the most is the MFC class library.
===================================================================================
OK, then see how we use ASP.
Everyone generally uses IIS's default scripting language VbScript as the server-side execution language of ASP. At the beginning, the VBS script and HTML are basically mixed together to achieve certain functions. For example, you need to display the latest 5 records on the current page, that's it.
1. Define the connection to the database first, such as:
| The following is the quoted content: db_path = ../database/cnbruce2005.mdb Set conn= Server.CreateObject(ADODB.Connection) connstr = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&Server.MapPath(db_path) conn.Open connstr |
2. Then create a database record collection to extract relevant information
| The following is the quoted content: Set rs = Server.CreateObject (ADODB.Recordset) sql = Select top 5 * from [news] order by n_id desc rs.Open sql,conn,1,1 |
3. Finally, display the data through loop method
| The following is the quoted content: do while not rs.eof response.write rs(n_title) rs.movenext loop 'There are also final shutdown and release operations rs.close set rs=nothing |
So here in response.write rs(n_title), for the final web design, it is estimated that some other HTML tag elements need to be added before and after it. So naturally, VBS scripts and HTML are mixed.
Let’s look at it again. If there are many pages that need to display these 5 records, then do you have to set each page like this? Of course, the most important thing is that the design of each page is different. Then this is the repetition. Repeat where? Can you please not repeat it?
1. For database connections, a database connection file conn.asp is directly established, and the content is as above.
2. So as long as you need to use the database and need to establish a connection, you can directly include the database connection file referenced.
<!--#include file=conn.asp-->
So what repetitions are saved above? Repeat each database connection. This is anti-duplication for some common codes. Let’s take a look at it. If I want this page to display 5 pieces and that page to display 6 pieces, what should I do? Obviously, this can only be a simple modification of the SQL statement in the current page, such as the original modification of top 5 to top 6.
OK, keep watching, are there any repetitions? Yes, except for the different definitions of SQL query, the rest are all duplicates.
So, continue to think of a solution: Can you define the number of extracted items yourself? I just want to extract only a few items, but the program only needs to write one. Then at this time, the function will come in handy. For example, I define a function like this:
| The following is the quoted content: Function topnews(tnum) Set rs = Server.CreateObject (ADODB.Recordset) sql = Select top &tnum& * from [news] order by n_id desc rs.Open sql,conn,1,1 do while not rs.eof response.write rs(n_title) rs.movenext loop rs.close set rs=nothing End Function |
Then, you can use topnews(5) or topnews(6) to complete the need
...At first glance, it seems that ASP uses functions and is finally completed. Because the main program function is made into a function module, you need to call it directly when using this function on the foreground page. If necessary, modify the function parameter value to be a perfect ending/.
So, why did classes be introduced in ASP? What is this type? And how to apply it?
In the ASP scripting language, VBscript has a Class keyword, which can be used to declare a custom class. for example
| The following is the quoted content: Class name statements End Class |
Here, public or private members can be declared in statements, including functions, members and attributes.
Javascript uses a function to declare a class, and then defines properties in the function through this.prototype and methods this.func defines.
Which one is simple to pick which one. After defining the name of the class, that is, the class, what can be done here?
The first one. MSDN documentation: In Visual Basic 6.0, when creating and destroying a class module, the class module uses the Initialize and Terminate events to perform all necessary operations. When an object is encountered for the first time after a New statement, the Initialize event is raised, and when the last reference to the object is released, the Terminate event is immediately raised. However, these event methods can be called directly at any time during execution.
So I often see examples like this:
Class cnbruce 'Declare a class named cnbruce
| The following is the quoted content: Private cnrose Private Sub Class_Initialize cnrose=My Name is cnrose. End Sub Private Sub Class_Terminate() End Sub End Class |
Look, this is an initialization.
In addition, there are let and get methods in the asp class. For example, add between the Class cnbruce of the above code... End Class:
| The following is the quoted content: Public Property get YName YName=cnrose End Property |
That is to become:
Class cnbruce 'Declare a class named cnbruce
| The following is the quoted content: Private cnrose Private Sub Class_Initialize cnrose=My Name is cnrose. End Sub Private Sub Class_Terminate() End Sub Public Property get YName YName=cnrose End Property End Class |
So how to extract the value, for example
| The following is the quoted content: Set aaa=New cnbruce response.write aaa.YName |
Note that aaa.YName already feels a little bit. aaa is an object defined as the cnbruce class. The final output is displayed as the YName get value in the object of this class. Its value content is the value of the variable cnrose, and the value has been initialized. Therefore, the final result is My Name is cnrose.
This is get, which directly extracts the inside of the already encapsulated class, so what about let? That is, external interactive access operations to classes. For example, I now apply the value defined externally to the class. That is, first define:
| The following is the quoted content: public property let MName(nnn) cnrose=nnn end property |
Its meaning is very simple. The value of the parameter nnn will be assigned to the variable cnrose according to different external values. Then the OK combination program, that is, the content of the displayed variable cnrose, is not the initialized content, but the uncertain value applied externally by let. Then how to apply let.
aaa.MName=sdasdasd
Just define the value directly. Then now look at the entire program:
| The following is the quoted content: <% Class cnbruce Private cnrose Private Sub Class_Initialize cnrose=My Name is cnrose. End Sub Public Property get YName YName=cnrose End Property public property let MName(nnn) cnrose=nnn end property End Class Set aaa=New cnbruce aaa.MName=hahahoho response.write aaa.YName %> |
Although the value of aaa.MName is defined as hahahoho, according to public property let MName(nnn), its value is to the variable cnrose. When SO outputs aaa.YName, according to Public Property get YName, it is not surprising that the result value is. /
As for functional programs, they are defined as function Function or subroutine Sub in the class.
SO, in general, is just a wrapper of functions, and it is easy to write and look good when applying (direct class name, method, class name, attribute-_-!), but don't expect it to have object-oriented thinking services like Java or .Net.
Time is limited, and the beginning and the end are just a little bit.
Share: Tips for using ASP to transfer HTML format data to Excel Learn how to build an ASP page to stream HTML data to an Execl spreadsheet and display the Execl spreadsheet in IE. So far, there are several ways to create Excel data tables using ASP technology, and you can also use server-side Excel 8.0 VBA groups