Text type single page ASP.NET counter

zhaozj2021-02-16  48

Mike Cat (Mikecat)

From: Old cat の Ide

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 a technical file because it is a single page counter, which 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. Let's pack the code for getting the counter file part into a function:

Function Mike_GetFileName () AS String

Dim Mike_Path As String

DIM MIKE_POSITION AS INTEGER

Mike_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 text of the text (including the character at Start), use the MID (Mike_Path, 1, Mike_Position) to end from the Start to the string using the MID (Mike_Path, 1, Mike_Position), and intercepted , Then connect our defined extensions to strings, get the count file name we need.

Then read the count file

Below we want to perform the read and write operations for the count file, first we use the previously defined Mike_GetFileName () function to get the current file name:

Dim Mike_Pathname As String

Mike_pathname = Mike_GetFileName ()

Instantiate a read channel for that file, that is, create 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.

Let's read the file 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, and then use the Readline () method to read the file content, you need special attention to

Mike_readobj.close ()

This statement is very important, otherwise the current count file has been exclusively by the StreamReader object, and the file will fail when the file is written, 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 = 1

Mikecat.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 required before writing a write operation:

Mike_Stream = New filestream (Mike_Pathname, FileMode.Open, FileAccess.write)

Since the count file already exists, we only use 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"%>

new asp.net document </ title></p> <p></ hEAD></p> <p><body bgcolor = "# ffffff" TopMargin = "0" marginheight = "0"></p> <p><script runat = "server" language = "vb"></p> <p>SUB Page_Load (Source As Object, E AS Eventargs)</p> <p>IF not page.ispostback then</p> <p>DIM Count As Integer</p> <p>DIM MIKE_STREAM As FileStream</p> <p>DIM MIKE_PATHNAME, MIKE_STR AS STRING</p> <p>Dim Mike_Readobj As StreamReader</p> <p>Dim Mike_Writerobj As Streamwriter</p> <p>Mike_pathname = Mike_GetFileName ()</p> <p>Mike_Stream = New filestream (Mike_Pathname, FileMode.Openorcreate, FileAccess.Read)</p> <p>Mike_readobj = new streamreader (Mike_Stream)</p> <p>Mike_str = Mike_Readobj.readline ()</p> <p>Mike_readobj.close ()</p> <p>Count = CINT (Mike_Str) Count = 1</p> <p>Mikecat.text = count</p> <p>Mike_Stream = New filestream (Mike_Pathname, FileMode.Open, FileAccess.write)</p> <p>Mike_writerobj = new streamwriter (Mike_Stream)</p> <p>Mike_writerobj.writeline (count)</p> <p>Mike_writerobj.close ()</p> <p>END IF</p> <p>End Sub</p> <p>Function Mike_GetFileName () AS String</p> <p>Dim Mike_Path As String</p> <p>DIM MIKE_POSITION AS INTEGER</p> <p>Mike_path = request.servervariables ("path_translated")</p> <p>Mike_position = Instrrev (Mike_Path, ".")</p> <p>Mike_getfilename = MID (Mike_Path, 1, Mike_Position) & "Count"</p> <p>END FUNCTION</p> <p></ script></p> <p><form runat = "Server" Method = ">></p> <p><asp: label id = "mikecat" runat = "server" /></p> <p></ form></p> <p></ body></p> <p></ html></p> <p>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. We use the <div id = "mike_div" runat = "server" /> We need 10 pictures, namely 0 ~ 9 ten numbers (name 0 ~ 9.gif), we use the following Method Dynamic Generating Images: DIM MIKE_IMG As Image</p> <p>DIM MIKE_GRAPH AS STRING</p> <p>DIM I as integer</p> <p>Count = CINT (Mike_Str) 1</p> <p>For i = 1 to len (count)</p> <p>Mike_img = new image ()</p> <p>MIKE_GRAPH = MID (count, i, 1)</p> <p>Mike_img.imageurl = mike_graph & ".gif"</p> <p>Mike_div.controls.add (mike_img)</p> <p>NEXT</p> <p>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. Ok, add this code to the counter code above it into a graphic counter. Ha ha. Try everyone! ~</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-23418.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="23418" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.057</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'J2X33WcvrQuyRMG3cZBR9A6EwMtIZsL4YpNOE_2Fgm3QjxlIK0KzeNlFsZlOjnvAhWIwvOOClAAebe9kLDPZFk7g_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>