[Reserved] text type single page ASP.NET counter

xiaoxiao2021-03-06  53

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"%>

new asp.net document </ title> </ head> <body bgcolor = "# ffffff" topmargin = "0" marginheight = "0"></p> <p><script runat = "server" language = "vb"></p> <p>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</p> <p></ script></p> <p><form runat = "server" method = "> <ask: label id =" mikecat "runat =" server "/> </ form> </ body> </ html></p> <p>========================================</p> <p>We will become a beautiful graphic counter here, and the two counters are exactly the same as file reading and writing.</p> <p>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 <div id = "mike_div" runat = "server" /></p> <p>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</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.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-115152.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="115152" 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.043</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 = '_2Fm42xZNvc6FSjT26UTZ9y6x4x_2B6Ty_2Fx3mKOBEqAire77exXlaWGm9OizpkLj2WuT6Ck7NrcQvqzQRwk2PtxJRg_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>