Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am developing a web application in that i want to count a hits for a particular page. Means if i have 2 asp pages like 1.aspx and 2.aspx then 3 people are visited 1.aspx so want to show visited people 3 in 1.aspx and if 2 people are visited 2.aspx then i want show visited people 2 in 2.aspx.

I tried Application variable but it is in global.asax so it showing same number of visitors. Please Help me.
Posted

1 solution

1.You should have different counters for each page, and store these counters in Application cache to be at the web application level;

2.In the Global.asax.cs init your used counter for each page like in the next example:
C#
void Application_Start(object sender, EventArgs e)
{
   Application["Page1Counter"] = 0;
   Application["Page2Counter"] = 0;
   //...
}

2.The logic for incrementing the counters for each page should be in the Page_Load event handler like in the next example:
C#
protected void Page1_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)   
    {
       int counter = (int)Application["Page1Counter"];//Get from the cache!
       counter++;
       Application["Page1Counter"] = counter; //Cache the result back!
    }
    //....
}

3.Example for your new question regarding only one page and multiple users:
C#
protected void Page1_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)   
    {
       string userID = User.Identity.Name; //Get the user ID!
       string key = string.Format("Page1CounterForUser{0}", userID);//Create the key for current user!
       int counter = (int)Application[key];//Get from the cache!
       counter++;
       Application[key] = counter; //Cache the result back!
    }
    //....
} 
 
Share this answer
 
v3
Comments
MAK09 23-Sep-14 7:19am    
Thank You Raul Iloc. As i am new to this u help me alot and once again thank u for instant reply
Raul Iloc 23-Sep-14 7:20am    
Welcome, I am glad that I could help you!
MAK09 23-Sep-14 7:20am    
I have one question that this for two pages what happened i have one page only. and the data shown on that page sgould b different as per user id. then how would i count. because this time page is same and data is different as per user requirement.
Raul Iloc 23-Sep-14 7:26am    
You should cache in your Application cache a key create based on the userID as key for the counter, like "string.Format("Page1CounterForUser{0}", userID);" and use these type of keys similar with "Page1Counter" in my solution above.
MAK09 23-Sep-14 7:29am    
Please can u give me demo because i am not getting how to use cache

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