See a very simple counter, perhaps it will be useful
I introduced the file operation object under System.io space, here I explain how to make a counter applying to a specific page. Each counter requires a corresponding file to store the current number of visits, so how to build a technology file, how to read and write the technical file and how to display the current number of visits is the problem we need to solve.
First get technical files
Because it is a single page counter, it is not applied to a specific page, so you must obtain or generate different technical files depending on the current page. It is best to use different extensions from the same name as the page.
We get the counter part of the code files packaged as a function: function mike_getfilename () as stringdim mike_path as stringdim mike_position as integermike_path = request.servervariables ( "path_translated") mike_position = instrrev (mike_path, ".") Mike_getfilename = mid (mike_path, 1, Mike_Position) & "Count" end function
Use the ServerVariables ("Path_Translated" method of the Request object to get the absolute path to the current page. The INSTRREV function returns a string from the position MID function that appears from the end in another string to return to the specified number of characters (String, Start [, Length]) parameter string: string expressions from the string: string expressions Returns the character from it. Returns NULL if string contains NULL. START: The start position of the character part of the sum of the string is extracted. If START exceeds the number of characters in String, the MID will return zero length string (""). Length: The number of characters to be returned. If omitted or longens exceeds the character number of text (including the character at Start), all characters that end from start to strings are returned in strings.
Using MID (Mike_Path, 1, Mike_Position) to intercept the file name. Then, then connect our defined extensions to the string, get the count file name we need.
Then read the count file
Below we want to perform is to read and write the count file, first we use the previously defined MIKE_GETFILENAME () function to get the current file name: DIM MIKE_PATHNAME AS STRINGMIKE_PATHNAME = Mike_GetFileName ()
Instantiate a read channel for that file, creating a FileStream object: Mike_Stream = new filestream (Mike_Pathname, FileMode.Openorcreate, FileAccess.Read)
Preventing an exception, the page is first accessed, and the count file is created using OpenorCreate. Next, the file is read using the StreamReader object: Mike_Readobj = New StreamReader (Mike_Stream) Mike_Str = Mike_Readobj.Readline () Mike_Readobj.close ()
Here we applied to the constructor of the StreamReader object, the constructor of the StreamReader object is generated, and then use the ReadLine () method to read the file content, you need to pay special attention to the mike_readobj.close () This statement is important, otherwise the current count file has been The StreamReader object is exclusively, and the file will fail when writing the file, so you must use the close () method to release the files and system resources occupied by the StreamReader object. Secondly show current visits
The current number of visits can be displayed by the response.write () method, the disadvantage of this method is that the display position of the counter cannot be adjusted. Here we use the server control Label in ASP.NET, set the TEXT attribute of the control in the business logic code area. This reflects the advantages of business logic and page logic code separation. However, after all, in a file, I personally recommend the code after the post page, the front page is placed on the ASPX file, and the background file is saved as a class file.
Count = cint (mike_str) count = 1mikecat.text = count
We convert the count files read by streamReader to a value, then count the value plus 1, and then display it in the Label control. Mikecat at this time is the Label control.
Last written counter file
A write channel is still needed before writing: Mike_Stream = new filestream (Mike_pathname, filemode.open, fileaccess.write) Since the count file already exists, we only use the FileMode.Open we use the constructor of the StreamWriter object. To generate a streamwriter object to perform file write: mike_writerobj = new streamwriter (mike_stream) mike_writerobj.writeline (count) mike_writerobj.close ()
Let's give all counter code, you can understand the details
<% @ Page language = "vb" debug = "true" explicit = "true"%> <% @ import namespace = "system.io"%>
Sub Page_Load mike_stream (Source As Object, E As EventArgs) If Not page.ispostback then Dim count As integer Dim mike_stream As filestream Dim mike_pathname, mike_str As string Dim mike_readobj As streamreader Dim mike_writerobj As streamwriter mike_pathname = mike_getfilename () = New filestream (mike_pathname , filemode.openorcreate, fileaccess.read) mike_readobj = New (mike_stream) streamreader mike_str = mike_readobj.readline () mike_readobj.close () count = cint (mike_str) count = 1 mikecat.text = count mike_stream = New filestream (mike_pathname, filemode .open, fileaccess.write) mike_writerobj = New streamwriter (mike_stream) mike_writerobj.writeline (count) mike_writerobj.close () End ifEnd subFunction mike_getfilename () As string Dim mike_path As string Dim mike_position As integer mike_path = request.servervariables ( "PATH_TRANSLATED" ) Mike_Position = INSTRRREV (Mike_Path, ".") M Ike_getfilename = MID (Mike_Path, 1, Mike_Position) & "Count" End Function
script>
form> body> html>========================================
We will become a beautiful graphic counter here, and the two counters are exactly the same as file reading and writing.
Controls for loading pictures in ASP.NET are image controls, so the method of dynamically generating image controls is implemented with dynamically generated graphics. This container we use
Here we need to prepare 10 pictures, 0 ~ 9 ten numbers (name 0 ~ 9.gif) Mike_str) 1for I = 1 to len (count) mike_img = new image () mike_graph = MID (count, i, 1) Mike_img.imageurl = mike_graph & ".gif" Mike_div.controls.add (mike_img) Next
First we decide the length of the graph that needs to be generated according to the length of the current count string. New instructions to dynamically generate image controls, and then specify the image path to display with the ImageURL property. Finally, use the ADD method of MIKE_DIV to add the currently generated image control to the container collection.