Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to calculate how many times a page has opened
is there any way to do so?
Posted

You can set the value in a cookie using js or asp, or in a session value (for a single user) or in application value (for all the users), is not necessary javascript.
You have to put this code server side on page load.


For all users:
Application["refresh_count"] = Convert.ToInt64(HttpContext.Current.Application["refresh_count"]) + 1;

For a single user with session:
Session["refresh_count"] = Convert.ToInt64(Session["refresh_count"]) + 1;

OR

Response.Cookies["UserSettings"]["refresh_count"] = Convert.ToInt64(Response.Cookies["UserSettings"]["refresh_count"]) + 1;
Response.Cookies["UserSettings"].Expires = DateTime.Now.AddDays(1d);
 
Share this answer
 
Here is a best way to achieve the same..

XML works fast and take less time to load..
XML
Place webuser control

C# code for webuser control

<pre lang="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class WebUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.countMe();
        DataSet tmpDs = new DataSet();
        tmpDs.ReadXml(Server.MapPath("~/hitcounter.xml"));
        lblCounter.Text = tmpDs.Tables[0].Rows[0]["hits"].ToString();
    }
    private void countMe()
    {
        DataSet tmpDs = new DataSet();
        tmpDs.ReadXml(Server.MapPath("~/counter.xml"));
        int hits = Int32.Parse(tmpDs.Tables[0].Rows[0]["hits"].ToString());
        hits += 1;
        tmpDs.Tables[0].Rows[0]["hits"] = hits.ToString();
        tmpDs.WriteXml(Server.MapPath("~/counter.xml"));
    }
}


Html code for webuser control

ASP.NET
<asp:Label ID="lblCounter" runat="server"></asp:Label>

Place xml file named hitcounter.xml
Paste this code in xml file
HTML
<counter>
<count>
<hits>0</hits>
</count>
</counter>

Drag & drop webuser contol in which page use want to add counter.
more info see below link..
http://www.c-sharpcorner.com/blogs/3956/hit-counter-in-asp-net.aspx[^]
 
Share this answer
 
v2
You can use free hit counters.

Free hit counters
 
Share this answer
 
Use Static variable to maintain count.

What exactly you want to do? If you want to check the no. of times your site is being opened,i guess global.asax application events will do plenty of work.

maintain the count in session_start or application_start events.
 
Share this answer
 
Comments
Hetal Jariwala 19-Mar-13 6:03am    
the link is there is there any api or web service which automatically calculate how many times till now it has been clicked?
Hetal Jariwala 19-Mar-13 6:03am    
i.e it is page how many time it has been opened

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900