我该如何创建计数器 [英] How Do I Create Counter

查看:88
本文介绍了我该如何创建计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Web应用程序,因为我想计算特定页面的点击量。意味着如果我有2个asp页面,如1.aspx和2.aspx然后3人访问1.aspx所以想要显示访问人3在1.aspx和如果2人访问2.aspx然后我想显示访问的人2在2.aspx。



我尝试了应用程序变量,但它在global.asax中,因此它显示了相同数量的访问者。请帮助我。

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.

推荐答案

1.每个页面应该有不同的计数器,并将这些计数器存储在应用程序缓存到Web应用程序级别;



2.在 Global.asax.cs init中为每个页面使用了计数器,如下例所示:

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:
void Application_Start(object sender, EventArgs e)
{
   Application["Page1Counter"] = 0;
   Application["Page2Counter"] = 0;
   //...
}



2.增加每个页面的计数器的逻辑应该在 Page_Load 事件中下一个示例中的处理程序:


2.The logic for incrementing the counters for each page should be in the Page_Load event handler like in the next example:

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。仅针对一个页面和多个用户的新问题的示例:


3.Example for your new question regarding only one page and multiple users:

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!
    }
    //....
} 


这篇关于我该如何创建计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆