当前位置:编程学习 > C#/ASP.NET >>

ASP.NET高手来看看

怎么我的程序里的Session.Abandon()方法不可以关闭Session呢?难道Session一定要超时才能完全关闭的吗?下面是我的代码...请各位指点下...

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;

public partial class Session_Welcome : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.lblTotal.Text = Application["total"].ToString();
this.lblOnLine.Text = Application["online"].ToString();
//string[] sz = (string[])Session["users"];// 定义一个数组sz读取Session中的users数组
//string name = sz[0];
//string pwd = sz[1];

if (Session["users"] != null) //判断是否为空
this.lblInfo.Text = "欢迎来到本网站,尊敬的" + ((string[])Session["users"])[0];
else
{
Response.Write("<script language='javascript'>alert('您还没登录,请登录!');</script>");
Response.Write("<script language='javascript'>window.location.href='Session_Login.aspx';</script>"); //没有登录,转向登录页面
}

//if (((string[])Session["users"])[0] != "admin" || ((string[])Session["users"])[1] != "admin")
//{
// Response.Write("<script language='javascript'>alert('用户名不存在或密码错误!');</script>");
// Response.Write("<script language='javascript'>window.location.href='Session_Login.aspx';</script>");
//}
}
}
protected void btnInfob_Click(object sender, EventArgs e)
{
this.lblType0.Text = Request.Browser.Browser;
this.lblCop0.Text = Request.Browser.Version;
this.lblIp0.Text = Request.UserHostAddress;
this.lblName0.Text = Request.UserHostName;
this.lblSys0.Text = Request.Browser.Platform;
if (Request.Browser.JavaScript == true)
this.lblJava0.Text = "支持";
else
this.lblJava0.Text = "不支持";
}
protected void btnInfos_Click(object sender, EventArgs e)
{
string pach = "";
this.lblSname0.Text = Server.MachineName;
this.lblPach0.Text = Server.MapPath(pach);
this.ListBox2.Items.Clear();
foreach (string item in Request.ServerVariables)
{
this.ListBox2.Items.Add(item);
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Session.Clear();//清空session里的数据
Session.Abandon();//关闭session
if (Session["users"] == null)
Response.Write("<script language='javascript'>alert('登出成功!');</script>");
Response.Write("<script language='javascript'>window.location.href='Session_Login.aspx';</script>");
}
}

以下是Global.asax里的代码

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

<script runat="server">

void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
Application.Lock();
Application["total"]=0;
Application["online"] = 0;
Application.UnLock();
}

void Application_End(object sender, EventArgs e)
{
//在应用程序关闭时运行的代码

}

void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码

}

void Session_Start(object sender, EventArgs e)
{
//在新会话启动时运行的代码

Session.Timeout = 1;
Application.Lock();
Application["total"] = Convert.ToInt32(Application["total"]) + 1;
Application["online"] = Convert.ToInt32(Application["online"]) + 1;
Application.UnLock();
}

void Session_End(object sender, EventArgs e)
{
//在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式
//设置为 StateServer 或 SQLServer,则不会引发该事件。
Application.Lock();
Application["online"] = Convert.ToInt32(Application["online"]) - 1;
Application.UnLock();
}

</script>

我是想实现在线人数的统计请各位看看为什么当我按了LinkButton1之后其中的在线人数没变化而总浏览数却加了1!!!!

这到底是为什么啊!!!!!!

补充:搞错了...是application...

void Session_End(object sender, EventArgs e)
{
//在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式
//设置为 StateServer 或 SQLServer,则不会引发该事件。
Application.Lock();
Application["online"] = Convert.ToInt32(Application["online"]) - 1;
Application.UnLock();
}

追问:RemoveAll()这个方法是关闭所有的Session吗?
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,