This is a very basic article describing that tracing an IPAddress and getting the number of clicks for a particular control. This gives the basic idea for counting the unique clicks for a control or for a page using XML.
This article contains only two pages. One is aspx and another one is XML file.
Problem Description: I have an image control in my page. I need to display the number of clicks for particular user system. This means for the first time when I click on the control, it should display like "no of clicks is 1" and for the second time onwards the value will vary.
Technologies: C#.Net, VISUAL STUDIO 2008
Pages: noofclicks.aspx, noofclicks.aspx.cs, count.xml
Code for noofclicks.aspx:
<form id="form1" runat="server">
<div>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/middle.jpg" onclick="ImageButton1_Click" />
</div>
</form>
Code behind for noofclicks.aspx page
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.Net;
using System.Data;
public partial class noofclicks : System.Web.UI.Page
{
XmlDocument doc = new XmlDocument();
int i;
DataSet ds;
int j;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
//retrieve ip address
string host = System.Net.Dns.GetHostName();
string hostname = Dns.GetHostEntry(host).HostName;
IPHostEntry ipEntry = Dns.GetHostEntry(host);
IPAddress[] addr = ipEntry.AddressList;
//xml
doc.Load(Server.MapPath("~/count.xml"));
XmlNodeList list = doc.SelectNodes("//NewDataset/section");
foreach (XmlNode node in list)
{
//checking whether the ip address is there or not
if (addr[0].ToString() == node.ChildNodes[1].InnerText)
{
i = Convert.ToInt32(node.ChildNodes[0].InnerText);
i += 1;
node.ChildNodes[0].InnerText = i.ToString();
doc.Save(Server.MapPath("~/count.xml"));
j = 1;
}
else
{
Join the conversation! Your thoughts help the community grow.