Add counters for. Text blogs

Source: Internet
Author: User

<%@ 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 = new Literal ();
Literal1.Text = "you are the first ";
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 = "guest. ";
PhImage. Controls. Add (literal );
}

</script>
<asp:PlaceHolder runat="server" ID="phImage"></asp:PlaceHolder>
It is indeed a very simple and very small function. It also modifies the database structure in the blog_Config table.
Add the PageVisitCount field. When using this field, you must modify the PageTemplate. ascx file and add
<% @ Register TagPrefix = "overflow" TagName = "DottextBlogCounter" Src = "~ /Skins/DottextBlogCounter. ascx "%>
Add appropriate locations
<Overflow: DottextBlogCounter id = "DottextBlogCounter" runat = "server"> </overflow: DottextBlogCounter>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.