Global.asax文件

Global.asax总共有7个事件,分别为:
Application_Start,Application_End,Application_BeginRequest,Application_EndRequest,Application_Error
Session_Start,Session_End

代码:
实现功能:1,利用Application对象定义一个全局变量 同时用Application_Start事件在应用程序开始被请求的时候就将其初始化 2,利用Application_BeginRequest统计访问量 3,利用Session_Start事件统计在线人数

Global.asax

<%@ Application Language=”C#” %>

<script runat=”server”>

void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
String Myconnstring = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + Server.MapPath(“.”) + “..\\data\\score.mdb;”;
Application[“ConnString”] = Myconnstring;
Application[“PageViewer_Count”]=0;
Application[“Online_Count”]=0;
}

void Application_BeginRequest(object sender, EventArgs e)
{
Application[“PageViewer_Count”] = (Int32)Application[“PageViewer_Count”] + 1;
}

void Session_Start(object sender, EventArgs e)
{
Application[“Online_Count”] = (Int32)Application[“Online_Count”] + 1;
}

void Session_End(object sender, EventArgs e)
{
Application[“Online_Count”] = (Int32)Application[“Online_Count”] – 1;
}

</script>

UseforGlobal.aspx

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”UseforGlobal.aspx.cs” Inherits=”UseforGlobal” %>
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.OleDb” %>

<!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”>
<script runat=server>
OleDbConnection Myconn;
public void Page_Load(Object src, EventArgs e)
{

string Myconnstring = (string)Application[“ConnString”];

Myconn = new OleDbConnection(Myconnstring);
Myconn.Open();

PageViewer_Count.Text = Application[“PageViewer_Count”].ToString();

Online_Count.Text = Application[“Online_Count”].ToString();

Data_Bind();
}

ICollection DataSource()
{
string strsel = “select * from score”;
OleDbDataAdapter MyAdapter = new OleDbDataAdapter(strsel, Myconn);
DataSet ds = new DataSet();
MyAdapter.Fill(ds, “score”);

return ds.Tables[“score”].DefaultView;

}

public void Data_Bind()
{
score.DataSource = DataSource();
score.DataBind();
}
</script>
<title>UseforGolbal</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:DataGrid ID=”score” runat=server>
<Columns>
<asp:BoundColumn HeaderText=”Name” DataField=”s_Name”/>
</Columns>
</asp:DataGrid><br><br>
PageViewer_Count:<asp:Label ID=”PageViewer_Count” runat=server/><br><br>
Online_Count:<asp:Label ID=”Online_Count” runat=server/>
</div>
</form>
</body>
</html>

1 对 “Asp.net Study(15)”的想法;

  1. Howdy! I know this is kinda off topic nevertheless I’d figured I’d ask. Would you be interested in trading links or maybe guest authoring a blog post or vice-versa? My blog goes over a lot of the same subjects as yours and I believe we could greatly benefit from each other. If you happen to be interested feel free to send me an email. I look forward to hearing from you! Wonderful blog by the way!

发表评论

邮箱地址不会被公开。 必填项已用*标注