新建一个网站,然后在Default.aspx里添加代码如下:
1<script language="c#" runat="server">
2 private void Page_Load(object sender, System.EventArgs e)
3 {
4 if (!IsPostBack)
5 GlobalGraph(MyPanel);
6 }
7 void GlobalGraph(Panel Container)
8 {
9 Label lbl;
10 Label lbl2;
11 string count;
12 lbl = new Label();
13 lbl2 = new Label();
14 lbl.Text = "该站点已被访问次数:";
15 Container.Controls.Add(lbl);
16 count = Application["Counter"].ToString();
17 for (int i = 0; i < count.Length; i++)
18 {
19 lbl2.Text=count.Substring(i, 1);
20
21 Container.Controls.Add(lbl2 ); //加载到控件容器
22 }
23 }
24 </script>
25
26<body style="background-color:White">
27<p><strong style=" font-family:@楷体_GB2312">Welcome to you!</strong> </p>
28 <form id="form1" runat="server">
29 <div>
30 <asp:Panel id="MyPanel" runat="server" width="50%" BorderColor="#C0FFC0" BorderStyle="Groove"></asp:Panel>
31 </div>
32 </form>
33</body>
添加新项Global.asax文件,先声名,添加代码如下:2 private void Page_Load(object sender, System.EventArgs e)
3 {
4 if (!IsPostBack)
5 GlobalGraph(MyPanel);
6 }
7 void GlobalGraph(Panel Container)
8 {
9 Label lbl;
10 Label lbl2;
11 string count;
12 lbl = new Label();
13 lbl2 = new Label();
14 lbl.Text = "该站点已被访问次数:";
15 Container.Controls.Add(lbl);
16 count = Application["Counter"].ToString();
17 for (int i = 0; i < count.Length; i++)
18 {
19 lbl2.Text=count.Substring(i, 1);
20
21 Container.Controls.Add(lbl2 ); //加载到控件容器
22 }
23 }
24 </script>
25
26<body style="background-color:White">
27<p><strong style=" font-family:@楷体_GB2312">Welcome to you!</strong> </p>
28 <form id="form1" runat="server">
29 <div>
30 <asp:Panel id="MyPanel" runat="server" width="50%" BorderColor="#C0FFC0" BorderStyle="Groove"></asp:Panel>
31 </div>
32 </form>
33</body>
<%@ Import Namespace="System.Web.SessionState" %>
<%@ Import Namespace="System.IO" %>
在相应位置添加代码如下:<%@ Import Namespace="System.IO" %>
void Application_Start(object sender, EventArgs e)
{
StreamReader rd = new StreamReader(Server.MapPath("counter.txt"));
int nNum = int.Parse(rd.ReadLine());
Application.Lock();
Application["Counter"] = nNum;
Application.UnLock();
rd.Close();
}
void Session_Start(object sender, EventArgs e)
{
Application.Lock();
Application["Counter"] = Convert.ToInt32(Application["Counter"]) + 1;
Application.UnLock();
//写入
StreamWriter sw = new StreamWriter(Server.MapPath("counter.txt"), false);//false为不追加
sw.WriteLine(Application["Counter"]);
sw.Close();
}
void Session_End(object sender, EventArgs e)
{
//写入
StreamWriter sw = new StreamWriter(Server.MapPath("counter.txt"),false);//false为不追加
sw.WriteLine(Application["Counter"]);
sw.Close();
}
最后你还要在程序目录里新建Counter.txt文件,就OK了
{
StreamReader rd = new StreamReader(Server.MapPath("counter.txt"));
int nNum = int.Parse(rd.ReadLine());
Application.Lock();
Application["Counter"] = nNum;
Application.UnLock();
rd.Close();
}
void Session_Start(object sender, EventArgs e)
{
Application.Lock();
Application["Counter"] = Convert.ToInt32(Application["Counter"]) + 1;
Application.UnLock();
//写入
StreamWriter sw = new StreamWriter(Server.MapPath("counter.txt"), false);//false为不追加
sw.WriteLine(Application["Counter"]);
sw.Close();
}
void Session_End(object sender, EventArgs e)
{
//写入
StreamWriter sw = new StreamWriter(Server.MapPath("counter.txt"),false);//false为不追加
sw.WriteLine(Application["Counter"]);
sw.Close();
}