ASP.NET
  Home arrow ASP.NET arrow Page 3 - Building a Counter Using JScript.Net
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Building a Counter Using JScript.Net
By:
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2002-07-12

    Table of Contents:
  • Building a Counter Using JScript.Net
  • How Should the Counter Work?
  • The JScript.Net Code
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building a Counter Using JScript.Net - The JScript.Net Code


    (Page 3 of 4 )

    First off, we need to create the functions which will read and save the counter (increase counter before if new visitor). Copy the following code into a new text file and save the file as counter.aspx:

    <%@ Page Language="JScript"%>
    <%@ Import Namespace="System.IO" %>
    <%

    var FilePath:String = Server.MapPath("\\") + "counter.txt";

    function getCounter():String
    {
    var SR:StreamReader = File.OpenText(FilePath,FileMode.Open);
    var Counter:String = SR.ReadLine().ToString();
    SR.Close();

    var Cookie:HttpCookie;
    Cookie = Request.Cookies("OldVisitor");

    if(Cookie==null)
    {
    var CounterInt:int = Convert.ToInt32(Counter);
    CounterInt++;
    Counter = Convert.ToString(CounterInt);

    var FS:FileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Write);
    var Text:StreamWriter = new StreamWriter(FS);
    Text.WriteLine(Counter);
    Text.Close();
    FS.Close();

    Cookie = new HttpCookie("OldVisitor","true");
    Cookie.Expires = DateTime.Now.AddSeconds(120);
    Response.AppendCookie(Cookie);
    }

    return Counter;
    }

    Response.Write(getCounter());
    %>


    You should now place the counter.aspx in your root folder (i.e. c:\InetPub\wwwroot) on your Internet Information Server. Also, create a file called counter.txt and set the read/write security options to full access for the user your are running under IIS. Open the counter.txt file, write a number (i.e. 999) and save the file.

    When you now open the URL http://localhost/counter.aspx in your browser, you will see a text counter. The counter has already add one count to the number you put in counter.txt.

    Now we want to replace the text counter with an image that we will create on the fly. Add the following code to counter.aspx before the "Response.Write(getCounter());" line:

    function drawCounter()
    {
    var height:int=20;
    var width:int=60;

    var bmp:Bitmap = new Bitmap(width, height);
    var img:Graphics = Graphics.FromImage(bmp);

    var white:SolidBrush = new SolidBrush(Color.White);
    var black:SolidBrush = new SolidBrush(Color.Black);

    var CurrentCounter:String = getCounter();

    var CounterFont:Font = new Font("Arial", 8, FontStyle.Bold);
    var Text:SizeF = img.MeasureString(CurrentCounter,CounterFont);

    img.FillRectangle(black, 0, 0, width, height);
    img.DrawString(CurrentCounter, CounterFont, white ,(bmp.Width)-((Text.Width)+5),3);

    bmp.Save(Response.OutputStream, ImageFormat.Jpeg);

    img.Dispose();
    bmp.Dispose();
    }


    Also, add the following namespaces to the namespace declarations at the beginning of the file:

    <%@ Import namespace="System.Drawing" %>
    <%@ Import namespace="System.Drawing.Imaging" %>
    <%@ Import namespace="System.Drawing.Drawing2D" %>

    And finally, replace "Response.Write(getCounter());" with "drawCounter();". Go back to your browser and refresh the page. You will see a graphical counter that increments when any new visitor comes to the page.

    More ASP.NET Articles
    More By


     

    ASP.NET ARTICLES

    - How Caching Means More Ca-ching, Part 2
    - How Caching Means More Ca-ching, Part 1
    - Reading a Delimited File Using ASP.Net and V...
    - What is .Net and Where is ASP.NET?
    - An Object Driven Interface with .Net
    - Create Your Own Guestbook In ASP.NET
    - HTTP File Download Without User Interaction ...
    - Dynamically Using Methods in ASP.NET
    - Changing the Page Size Interactively in a Da...
    - XML Serialization in ASP.NET
    - Using Objects in ASP.NET: Part 1/2
    - IE Web Controls in VB.NET
    - Class Frameworks in VB .NET
    - Cryptographic Objects in C#: Part 1
    - Sample Chapter: Pure ASP.Net







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway