欢迎光临
我们一直在努力

asp.net中用C#实现站点计数器用户控件-.NET教程,C#语言

建站超值云服务器,限时71元/月

----------------

资源:同级目录下的存放当前计数的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="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=\"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

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » asp.net中用C#实现站点计数器用户控件-.NET教程,C#语言
分享到: 更多 (0)