asp.net中用C#实现站点计数器用户控件
2024-07-10 13:00:14
供稿:网友
----------------
资源:同级目录下的存放当前计数的count.txt文件
子目录pic下的0到9的数字图片0.gif ~ 9.gif
--------------------------------
asax文件:
<%@ control language="c#" autoeventwireup="false" codebehind="counter.ascx.cs" inherits="jianweb.counter" targetschema="http://schemas.microsoft.com/intellisense/ie5"%>
<link href="css.css" rel="stylesheet">
<font face="宋体">
<table id="table_counter" cellspacing="0" cellpadding="0" width="750" align="center" border="0"
runat="server">
<tr>
<td style="height: 23px" align="center" valign=middle><img height="1" alt="" src=http://www.163design.net/n/c/"pic/rightblueback.gif" width="700"></td>
</tr>
<tr>
<td align=center valign=middle></td>
</tr>
</table>
</font>
-------------------
.cs文件:
namespace jianweb
{
using system;
using system.data;
using system.drawing;
using system.web;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
/// <summary>
/// counter 的摘要说明。
/// </summary>
public class counter : system.web.ui.usercontrol
{
protected system.web.ui.htmlcontrols.htmltable table_counter;
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
string filepath=system.web.httpcontext.current.server.mappath("hits.txt");
system.io.streamreader srreadline = new system.io.streamreader(
system.io.file.openread(filepath),
system.text.encoding.ascii);//encoding.default是读中文
srreadline.basestream.seek(0, system.io.seekorigin.begin);//
string countstr="";
if (srreadline.peek() > -1)
{
countstr+=srreadline.readline();
}
int count=int.parse(countstr)+1;
countstr=count.tostring();
srreadline.close();
table_counter.rows[1].cells[0].innerhtml="<font color=/"#009900/">";
for (int i=0;i<countstr.length;i++)
{
table_counter.rows[1].cells[0].innerhtml=table_counter.rows[1].cells[0].innerhtml+"<img src=http://www.163design.net/"pic//"+countstr.substring(i,1)+".gif/">";
}
table_counter.rows[1].cells[0].innerhtml+="</font>";
// write the string to a file.
system.io.streamwriter file = new system.io.streamwriter(filepath);
file.writeline(countstr,false);
file.close();
srreadline.close();
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}