In this article I’ll show you how to make Users total no. of visits to any website with the help of application state in asp.net using c#, we also use global.asax file here.
Global.asax file is the asp.net application file that contains the code for application state level events raised by the asp.net or http modules. Global.asax is resides in the root directory of asp.net.
INITIAL CHAMBER:
Step1) Open Your Visual Studio 2010 and Create an Empty Website, Give a suitable name [Applicationstate_demo].
Step2) In Solution Explorer you get your empty website, then add a Web Form and Global.asax file. By going like this –
For Web Form:
applicationstate_demo (Your Empty Website) -> Right Click -> Add New Item -> Web Form. Name it as -> applicationstate.aspx.
For Global.asax:
applicationstate_demo (Your Empty Website) -> Right Click -> Add New Item - -> Global Application Class.
Global.asax Code:
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["VisitorCount"] = 0;
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application["VisitorCount"] = (int)Application["VisitorCount"] + 1;
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
As Application start we have visitor count = 0 ,as we go ahead we increment the visitor count to +1.
As the session start we make the visitor count to +1 in the event of session start.
This is my welcome page you can make your own or you can just make it blank and print the label for total visiting.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
text-align: center;
}
.style2
{
font-size: xx-large;
}
</style>
</head>
<body bgcolor="#ffffcc">
<form id="form1" runat="server">
<strong><span class="style2">Welcome to Active DotNet<br />
<br />
</span></strong><br />
<br />
<br />
<br />
<asp:Image ID="Image1" runat="server" Height="350px" ImageUrl="~/1.jpg" />
<br />
<br />
</div>
<p>
<asp:Label ID="Label2" runat="server"
style="text-align: center; font-weight: 700; font-size: large"></asp:Label>
</p>
</form>
</body>
</html>
Here in the label we will print out the total visiting. Look below image.
Hope you like this. Have a nice day. Thank you for Reading.
No comments:
Post a Comment