C#的一个程序错了 帮帮忙

来源:百度知道 编辑:UC知道 时间:2024/05/30 04:23:45
protected void Page_Load(object sender, EventArgs e)
{
int Countter;
Application. Lock();
Application .Set ("Countter",Application .Count+1);
Application .UnLock ();
Label1.Text = "您是第" + Application .Count + "位访客";
}

要怎么写才更好

protected void Page_Load(object sender, EventArgs e)
{
string Countter=null;
Application.Lock();
Application.Add(Countter, 0);
Application[Countter] = (int)Application[Countter] + 1;
Application.UnLock();
Label1.Text = "您是第" + Application[Countter] + "位访客";
}
这样改,每次刷新自动增1,没问题了吧

或者
新建一个Global.asax页面
按如下修改
void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
Application["countter"] = 0;
}
void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
Application.Lock();
Application["countter"] = (int)Application["countter"] + 1;
Application.UnLock();
}
后台cs
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "您是第" + Application["countter"].ToString()