为.Text Blog 添加 计数器
<%@ control language="C#" classname="DottextBlogCounter" inherits="Dottext.Web.UI.Controls.BaseControl"%>
<%@import namespace="System.Data" %>
<%@import namespace="System.Data.SqlClient" %>
<%@import namespace="Dottext.Framework.Data"%>
<script runat="server">
// DottextBlogCounter created on 4/25/2005 by:
//*daqingshu http://blog.csdn.net/daqingshu
/// <summary>
/// Use session or cookie?
/// </summary>
void Page_Load(object sender, System.EventArgs e)
{
string number = "";
string strSQL = "";
string conn = Dottext.Framework.Providers.DbProvider.Instance().ConnectionString;
HttpCookie oldCookie = Request.Cookies["counter"];
if(oldCookie == null)//if cookie exist,do not count
{
HttpCookie cookie = new HttpCookie("counter");
DateTime dtNow = DateTime.Now;
TimeSpan tsMinute = new TimeSpan(0, 1, 0, 0);//cookie expire time 1 hour
cookie.Expires = dtNow + tsMinute;
Response.Cookies.Add(cookie);
strSQL = "UPDATE blog_Config set PageVisitCount = PageVisitCount + 1";
SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSQL, null);
}
strSQL = "SELECT PageVisitCount FROM blog_Config WHERE BlogID = " + Convert.ToInt32(CurrentBlog.BlogID);
SqlDataReader rdr = SqlHelper.ExecuteReader(conn, CommandType.Text, strSQL, null);
if(rdr.Read()){
number = rdr[0].ToString();
}
rdr.Close();
if(number.Length <= 8)
{
for(int i = 8; i >= number.Length; --i)
{
number = "0" + number;
}
}
NumberToPicture(number);
}
private void NumberToPicture(string number)
{
System.Web.UI.WebControls.Literal Literal1 = new Literal();
System.Web.UI.WebControls.Literal Literal2 = new Literal();
Literal1.Text = "您是第";
phImage.Controls.Add(Literal1);
if(number.Length > 0)
{
for(int i = 0; i < number.Length; ++i)
{
StringBuilder strImage = new StringBuilder();//maybe I missing the System.text namespace if it doesnt work
strImage.Append("~/Images/counter/eggBlue/");
strImage.Append(number[i]);
strImage.Append(".gif");
System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
image.ID = "image" + i.ToString();
image.ImageUrl = strImage.ToString();
phImage.Controls.Add(image);
}
}
Literal2.Text = "位访客。";
phImage.Controls.Add(Literal2);
}
</script>
<asp:PlaceHolder runat="server" ID="phImage"></asp:PlaceHolder>
的确是个很简单很小的功能,还修改了数据库结构,在表blog_Config里面
添加PageVisitCount字段,使用的时候,需要修改PageTemplate.ascx文件,在里面添加
<%@ Register TagPrefix="overflow" TagName="DottextBlogCounter" Src="~/Skins/DottextBlogCounter.ascx" %>
合适的位置添加
<overflow:DottextBlogCounter id="DottextBlogCounter" runat="server"></overflow:DottextBlogCounter>
补充:Web开发 , ASP.Net ,