Below is the program which will show you how to maintain user page visit counts for web page using cookies. After each manual page refresh the count goes on increasing. Here us the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace cookie_example
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int x = 0;
try
{
HttpCookie c = Request.Cookies["mycookie"];
if(c!= null)
{
String str = c.Value.ToString();
x = Int32.Parse(str);
}
}catch(Exception ex)
{
Response.Write("Error:"+ex);
}
x++;
HttpCookie c1 = new HttpCookie("mycookie",x.ToString());
Response.Cookies.Add(c1);
Response.Write("You have visited"+x.ToString()+" times....");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace cookie_example
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int x = 0;
try
{
HttpCookie c = Request.Cookies["mycookie"];
if(c!= null)
{
String str = c.Value.ToString();
x = Int32.Parse(str);
}
}catch(Exception ex)
{
Response.Write("Error:"+ex);
}
x++;
HttpCookie c1 = new HttpCookie("mycookie",x.ToString());
Response.Cookies.Add(c1);
Response.Write("You have visited"+x.ToString()+" times....");
}
}
}
Comments
Post a Comment