您是第位访客。如何实现这种效果呢?
使用Global.asax全局变量可以避免刷新页面导致计数器刷新。
------------------------
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码,IIS重新启动时刷新计数器。
Application["count"] = 0;
}
void Session_Start(object sender, EventArgs e)
{
//在新会话启动时运行的代码,会话建立,计数器+1,防止错误先锁定后解锁。重复刷新页面不会更改计数器,只有建立新会话才可以。
Application.Lock();
Application["count"] = (int)Application["count"] + 1;
Application.UnLock();
}
--------------------------------
在页面添加几个image控件,我是添加在母版页上的*.master。
--------------------------------
<asp:Image ID="Image5" runat="server" />
<asp:Image ID="Image4" runat="server" />
<asp:Image ID="Image3" runat="server" />
<asp:Image ID="Image2" runat="server" />
<asp:Image ID="Image1" runat="server" />
-----------------------------------
在页面*.master.cs的Page_Load方法添加如下内容
-----------------------------------
protected void Page_Load(object sender, EventArgs e)
{
//Application.Lock();
//Application["count"] = (int)Application["count"] + 1;
//Application.UnLock();
//Label1.Text = "您是第" + Application["count"].ToString() + "位访问者";
string vistorNumbers = Application["count"].ToString();
switch (vistorNumbers.Length)
{
case 1:
Image5.ImageUrl = @".\Images\vist\0.gif";
Image4.ImageUrl = @".\Images\vist\0.gif";
Image3.ImageUrl = @".\Images\vist\0.gif";
Image2.ImageUrl = @".\Images\vist\0.gif";
Image1.ImageUrl = @".\Images\vist\" + vistorNumbers + ".gif";
break;
case 2:
Image5.ImageUrl = @".\Images\vist\0.gif";
Image4.ImageUrl = @".\Images\vist\0.gif";
Image3.ImageUrl = @".\Images\vist\0.gif";
Image2.ImageUrl = @".\Images\vist\" + vistorNumbers.Substring(0, 1) + ".gif";
Image1.ImageUrl = @".\Images\vist\" + vistorNumbers.Substring(1) + ".gif";
break;
case 3:
Image5.ImageUrl = @".\Images\vist\0.gif";
Image4.ImageUrl = @".\Images\vist\0.gif";
Image3.ImageUrl = @".\Images\vist\" + vistorNumbers.Substring(0, 1) + ".gif";
Image2.ImageUrl = @".\Images\vist\" + vistorNumbers.Substring(1, 1) + ".gif";
Image1.ImageUrl = @".\Images\vist\" + vistorNumbers.Substring(2) + ".gif";
break;
case 4:
Image5.ImageUrl = @".\Images\vist\0.gif";
Image4.ImageUrl = @".\Images\vist\" + vistorNumbers.Substring(0, 1) + ".gif";
Image3.ImageUrl = @".\Images\vist\" + vistorNumbers.Substring(1, 1) + ".gif";
Image2.ImageUrl = @".\Images\vist\" + vistorNumbers.Substring(2, 1) + ".gif";
Image1.ImageUrl = @".\Images\vist\" + vistorNumbers.Substring(3) + ".gif";
break;
case 5:
Image5.ImageUrl = @".\Images\vist\" + vistorNumbers.Substring(0, 1) + ".gif";
Image4.ImageUrl = @".\Images\vist\" + vistorNumbers.Substring(1, 1) + ".gif";
Image3.ImageUrl = @".\Images\vist\" + vistorNumbers.Substring(2, 1) + ".gif";
Image2.ImageUrl = @".\Images\vist\" + vistorNumbers.Substring(3, 1) + ".gif";
Image1.ImageUrl = @".\Images\vist\" + vistorNumbers.Substring(4) + ".gif";
break;
default: goto case 5;//待验证
}
}
---------------------------
@".\Images\vist\" 的意思很明显,事先把0~9是个gif图片保存到该路径下。没想到其它好方法~~
参考资料:
http://www.cnblogs.com/shanymen/archive/2009/05/13/1456253.html