Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

ASP.net 2.0 i brojač

[es] :: .NET :: ASP.NET :: ASP.net 2.0 i brojač

[ Pregleda: 650 | Odgovora: 3 ]

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

dmd

Član broj: 37299
Poruke: 125
*.ptt.yu.



Profil

icon ASP.net 2.0 i brojač29.05.2007. u 18:07

Kako u asp.net-u napraviti da se tačno zna koja stranica je koliko puta od korisnika otvorena?
Predpostavljam da se napravi tekst fajl i pri događaju page_load uvećava broj za jedan ili
ima svrsishodnija procedura?

29.05.2007. u 18:07 

bjevta
Bratislav Jevtic
http://www.tojesoft.net/Doc/BratislavJevtic-CV-EN.pdf
Kragujevac

Član broj: 5216
Poruke: 163
*.dynamic.sbb.co.yu.

Sajt: www.tojesoft.net


Profil

icon Re: ASP.net 2.0 i brojač29.05.2007. u 20:53
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]
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
29.05.2007. u 20:53 

dmd

Član broj: 37299
Poruke: 125
*.dynamic.sbb.co.yu.



Profil

icon Re: ASP.net 2.0 i brojač30.05.2007. u 10:31
Hvala.Probaću.
30.05.2007. u 10:31 

Drasko M
Beograd

Član broj: 136941
Poruke: 27
*.adsl.beotel.net.

Sajt: www.direktnaprodaja.com


Profil

icon Re: ASP.net 2.0 i brojač30.05.2007. u 16:22
Napravis Application objekte za svaku stranu i povecavas im vrednost za 1 kada se otvara stranica a nije postback-ovana?
30.05.2007. u 16:22 

[es] :: .NET :: ASP.NET :: ASP.net 2.0 i brojač

[ Pregleda: 650 | Odgovora: 3 ]

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.