// If you want to see the counter increase for every hit on the page, please change the check // in page_load from the session object to the page.ispostback. Read more about the firefox bug that made me // change the logic in this way below. // Warning!!! For some obscure reason, the pageload event is firing twice in Firefox. More about that // here: http://stackoverflow.com/questions/2153579/page-load-fires-twice-on-firefox. Despite my efforts (disabling // all firefox extensions for example), the event is still firing twice and the img tag that is generated by ASP // does not have the common error of missing an img attribute. So, in firefox, every reload of the page causes // the counter used to increase by 2 if I used PostBack to check if the user is new. Therefore I enabled // a session variable that checks if the user is already counted. That means that simple reload of the page // will not increase the counter. You have to use a different browser or close the current one and open the page // again to increase it. Bear in mind that if you stopped the server, the counter will be reset (I have implemented // this functionality via Global.asax Application_Start event. protected void Page_Load(object sender, EventArgs e) { var context = new DataContext(); var data = context.Data.SingleOrDefault(); if (data == null) { var visitorData = new VisitorData(); visitorData.Number++; context.Data.Add(visitorData); context.SaveChanges(); return; } if (Session["Catched"] == null) //(!Page.IsPostBack) { Session["Catched"] = "catched"; data.Number++; context.SaveChanges(); } context.Dispose(); Response.Clear(); GetImage(); }
protected void Application_Start(object sender, EventArgs e) { var context = new DataContext(); var data = context.Data.SingleOrDefault(); if (data == null) { var visitorData = new VisitorData(); context.Data.Add(visitorData); context.SaveChanges(); return; } else { data.Number = 0; context.SaveChanges(); } }