p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

aspdotnet_website_programming thread: Count how many times a link is hitten!


Message #1 by "Lampros Eleftherios" <lampros@c...> on Thu, 16 May 2002 22:09:24 +0300
Hello,
I would like to count how my times a hypelink was clicked. Assume that you
have a table in a web page like the next one:
____________________________________________________________________________
______________
|   http://this_is.my.hypelink                                          |
Number of times the link is hitten                |
|_________________________________________|_________________________________
_______________|_

Let's say that you click the link above and in the right cell is showed the
number of hits. Every time that you hit the link
the number increases. I want to store the number in a database and then
present it at the right cell of my table.

Any ideas?

Thanx a lot
Terry

Message #2 by Feduke Cntr Charles R <FedukeCR@m...> on Thu, 16 May 2002 14:24:01 -0400
Terry,

	Yeah, you'd need to do something like this:

1. Your link would need to become a LinkButton, i.e.:
<asp:LinkButton id="link" RunAt="server" Text="here's my link!" />

2. You'll need to add a boolean state variable to determine if the (or any)
link has been clicked for redirection action:

protected bool linkClicked = false;

3. You need to define an event to increment a counter in the database:

private void link_Click(object Sender, EventArgs e)
{
	// incremenet the count here
	
	// a link was clicked
	this.linkClicked = true; 
}

4. You'd need to tie the link button to a specific event in the
InitializeComponent() event:

this.link.Click += new System.Web.UI.ClickEventHandler(link_Click);

5. I think you can do this in the Page_Load, but maybe you need to do it in
a different event: if linkClicked == true, then they should be redirected.

if (this.linkClicked && Page.IsPostBack)
	Response.Redirect(link.Href);

6. And then to display the count, you'd do something like this in the
Page_Load:

if (!Page.IsPostBack)
{
	// get the count from the database here
}

	If this is going to be common functionality, I _strongly_ recommend
you write this as a web control, not on a case-by-base basis.

HTH,
- Chuck

-----Original Message-----
From: Lampros Eleftherios [mailto:lampros@c...]
Sent: Thursday, May 16, 2002 3:09 PM
To: Website Programming with ASP.NET
Subject: [aspdotnet_website_programming] Count how many times a link is
hitten!
Importance: High


Hello,
I would like to count how my times a hypelink was clicked. Assume that you
have a table in a web page like the next one:
____________________________________________________________________________
______________
|   http://this_is.my.hypelink                                          |
Number of times the link is hitten                |
|_________________________________________|_________________________________
_______________|_

Let's say that you click the link above and in the right cell is showed the
number of hits. Every time that you hit the link
the number increases. I want to store the number in a database and then
present it at the right cell of my table.

Any ideas?

Thanx a lot
Terry



  Return to Index