欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【ASP.NET结合XML编写计数器】,下面是详细的分享!
ASP.NET结合XML编写计数器
Code:
1) counter.aspx :- The Counter Page
<%@ Import Namespace="System.IO" %>
<%@ Assembly Name="System.xml(标准化越来越近了)" %>
<%@ Import Namespace="System.xml(标准化越来越近了)" %>
<%@ page language="c#" EnableSessionState="True" %>
<%-- These are the imported assembiles and namespaces need to run the counter --%>
<html>
<head>
<title>Saurabh's xml(标准化越来越近了) Counter Script</title>
<script language="C#" runat="server">
//script is called when the page is loaded
public void Page_Load(Object src, EventArgs e)
{
//the path to the xml(标准化越来越近了) file which will contain all the data
//modify this if you have any other file or directory mappings.
//modify this if you have been directed here from Step 2 of the ReadMe file.
string datafile="db/xml(标准化越来越近了)counter.xml(标准化越来越近了)" ;
if(!Page.IsPostBack){
//try-catch block containing the counter code
try {
//create an instance of the class xml(标准化越来越近了)Document
xml(标准化越来越近了)Document xml(标准化越来越近了)document=new xml(标准化越来越近了)Document() ;
//Open a FileStream to the specified file
FileStream fin ;
//It is very Important to specify the "FileShare.ReadWrite" option.
//This allows other viewers to also read and write to the Database
//This was missing in my last release hence there was a BUG !!!
fin=new FileStream(Server.MapPath(datafile), FileMode.Open, FileAccess.Read,
FileShare.ReadWrite) ;
//Load the Document
xml(标准化越来越近了)document.Load(new StreamReader(fin)) ;
fin.Close();
//create an instance of the DocumentNavigator class used to
//navigate through and xml(标准化越来越近了) file
DocumentNavigator navigator=new DocumentNavigator(xml(标准化越来越近了)document) ;
//Move to the first element (in my file 'Visitors')
navigator.MoveToDocumentElement() ;
//move to it child at position '0' (ie.in my file 'total' node)
navigator.MoveToChild(0) ;
//check if we are on the right element which has an attribute
if (navigator.HasAttributes) {
//get the attribute of the node 'total' called 'tot' (see the xml(标准化越来越近了)counter.xml(标准化越来越近了) file)
//since the value stored is in a string format we 'cast' it into a Int type
int total=int.Parse(navigator.GetAttribute("tot")) ;
//increase the counter
total++ ;
//show the counter on the page
countmess.Text="You are visitor N "+total.ToString() ;
//save the incremented counter back in the xml(标准化越来越近了) file
navigator.SetAttribute(0,total.ToString() );
}
//Update the Database only if a new session is there
if(Session["counter"]==null)
{
//move back to the Document element
navigator.MoveToDocumentElement() ;
navigator.MoveToChild(0) ;
//then insert the element after the 'total' element which will contain all
//the information of a single visitor
navigator.Insert(TreePosition.After , xml(标准化越来越近了)NodeType.Element, "Viewer","","") ;
//make an instance to the HttpUrl class to get information of the referrer to
//the page if any. if there are no referrers then by Default this object is 'null'
//so we have to make a check if it is null and do the needful
HttpUrl objUrl=Request.UrlReferrer;
if(objUrl!=null)
{
navigator.Insert(TreePosition.FirstChild, xml(标准化越来越近了)NodeType.Element,"Referrer","","");
navigator.Insert(TreePosition.FirstChild, xml(标准化越来越近了)NodeType.Text,"Referrer","","") ;
以上所分享的是关于ASP.NET结合XML编写计数器,下面是编辑为你推荐的有价值的用户互动:
相关问题:asp.net网站首页加计数器的代码如何编写?
答:想法而已 统计IP 的话 就判断IP 更新文件 或数据库 累加1 统计PV 的话 页面刷新 就更新 文件 或数据 累加1 >>详细
相关问题:asp.net如何编写一个图片访问计数器
答:你可以编写一个httpModule,用来监听所有访问文件的后缀名为jpg gif等图片文件的路径。在编写HttpModule里面,对对应访问的图片路径进行分类后进行计数。 >>详细
相关问题:关于asp.net的网站计数器
答: void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 Application["StatCount"] = GetDB.SelectData("PROC_SELTOTLA").Tables[0].Rows[0][0].ToString(); Application["onlineWhx"] = 0; } void Application_... >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
