Ovako:
1. Napravi user kontrolu, nazovije je, na primer, WucCounter
2. U code behind copy/past ovo što sam naveo pod WucCounter.cs
3. U aspx file copy/paste ovo što sam naveo pod WucCounter.aspx
4. CSS u aspx (CssClass="cntnumber") podesi po nahođenju
5. Ovo log.Error("xxx", ex); je od log4net open source projekta - za početak stavi pod komentar
To bi bilo to.
WucCounter.cs:
------------------------------------------------------
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class UserControls_WucCounter : System.Web.UI.UserControl {
protected static readonly log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
DoCount();
}
}
protected void DoCount() {
HttpCookie cookie = Request.Cookies[COOKIE_NAME];
GetCounter();
if (cookie == null) {
cookie = new HttpCookie( COOKIE_NAME, DateTime.Now.ToString());
cookie.Expires = DateTime.Now.AddHours(1);
IncCounter();
} else {
}
vCnt.Text = cnt.ToString("0000000");
Response.Cookies.Add(cookie);
}
protected int cnt = 1;
protected const string COOKIE_NAME = "LastVisit";
protected const string RELATIVE_FILE_NAME = "App_Data/counter.txt";
protected string FileName { get { return Server.MapPath(RELATIVE_FILE_NAME); } }
protected void GetCounter() {
try {
StreamReader sr = new StreamReader(File.Open(FileName, FileMode.OpenOrCreate));
string sCnt = sr.ReadLine();
sr.Close();
cnt = sCnt != null ? int.Parse(sCnt) : cnt;
} catch (Exception ex) {
log.Error("GetCounter", ex);
}
}
protected void IncCounter() {
cnt++;
try {
StreamWriter sw = new StreamWriter( File.OpenWrite( FileName ) );
sw.WriteLine(cnt.ToString());
sw.Close();
} catch (Exception ex) {
log.Error("IncCounter", ex);
}
}
}
======================================
WucCounter.aspx:
-------------------------------------------------
Code:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WucCounter.ascx.cs" Inherits="WucCounter" %>
<asp:Label ID="vCnt" CssClass="cntnumber" runat="server" Text="00000"></asp:Label>
[Ovu poruku je menjao bjevta dana 31.05.2007. u 23:14 GMT+1]