所以我想了解的是整个global.asax事件。 我正在做一个记录网站访问的简单计数器。我正在使用MSSQL。关于global.asax和那里的事件
基本上我有两个整数。 totalNumberOfUsers - 从开始的总visist。 currentNumberOfUsers - 此刻查看该网站的用户总数。
我是这么理解的Global.asax事件的方式是,每次有人来“在session_start”被触发一次该网站。所以每个用户一次。 “Application_Start”仅在某人第一次来到该站点时被触发。
跟这个我有我的global.asax文件在这里。
<script runat="server">
string connectionstring = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application.Lock();
Application["currentNumberOfUsers"] = 0;
Application.UnLock();
string sql = "Select c_hit from v_counter where (id=1)";
SqlConnection connect = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(sql, connect);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Application.Lock();
Application["totalNumberOfUsers"] = reader.GetInt32(0);
Application.UnLock();
}
reader.Close();
cmd.Connection.Close();
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application["totalNumberOfUsers"] = (int)Application["totalNumberOfUsers"] + 1;
Application["currentNumberOfUsers"] = (int)Application["currentNumberOfUsers"] + 1;
Application.UnLock();
string sql = "UPDATE v_counter SET c_hit = @hit WHERE c_type = 'totalNumberOfUsers'";
SqlConnection connect = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(sql, connect);
SqlParameter hit = new SqlParameter("@hit", SqlDbType.Int);
hit.Value = Application["totalNumberOfUsers"];
cmd.Parameters.Add(hit);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
Application.Lock();
Application["currentNumberOfUsers"] = (int)Application["currentNumberOfUsers"] - 1;
Application.UnLock();
}
</script>
在Page_Load我有这个
protected void Page_Load(object sender, EventArgs e)
{
l_current.Text = Application["currentNumberOfUsers"].ToString();
l_total.Text = Application["totalNumberOfUsers"].ToString();
}
所以,如果我理解这个权利,每次有人来两个currentNumberOfUsers和totalNumberOfUsers递增与1但当会议结束网站该currentNumberOfUsers与1
递减,如果我去到现场与3种在同一台计算机的浏览器,我应该在这两个柜台命中有3个。在几个小时后再次这样做,我应该有3个和总共6个,对吧?
其工作,现在是当前的方式上升到2,总递增对IE和Chrome每次回发,但不是在Firefox。
还有最后一件事,这是一回事吗?
Application["value"] = 0;
value = Application["value"]
//OR
Application.Set("Value", 0);
Value = Application.Get("Value");
这会给我来的用户总数。当前用户查看该网站的情况如同正在运行多少个会话一样。我会使用应用程序设置? – eski 2010-03-22 10:18:53