在VS2003里面global.asax里面直接有session_Start 但2005里面只有Application_Start,
请教下 2005里面怎么处理啊?

解决方案 »

  1.   

    一个是程序关闭和开始的时候是application
    session是对话撒
    你直接设置session就行了
    然后在webconfig里把
    <system.web>
    <sessionState mode="InProc">
    </sessionState>
    设置了才能生效
    给你个例子先定义一个Global.asax
    代码如下
    <%@ Application Language="C#" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %><script runat="server">
        string strcon =ConfigurationManager.AppSettings["sa"];
        void Application_Start(object sender, EventArgs e)
        {
           
            Application["online"] = 0;
            Application["lishi"] = 0;
            //在应用程序启动时运行的代码    }
        
        void Application_End(object sender, EventArgs e) 
        {
            //在应用程序关闭时运行的代码    }
            
        void Application_Error(object sender, EventArgs e) 
        { 
            //在出现未处理的错误时运行的代码    }    void Session_Start(object sender, EventArgs e)
        {
            Session.Timeout = 1;
            SqlConnection cn = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand("select * from Count", cn);
            cn.Open();
            int cout = Convert.ToInt32(cmd.ExecuteScalar());
            cn.Close();
            Application.Lock();
            Application["online"] = (int)Application["online"] + 1;
            Application["lishi"] = cout + 1;
            Application.UnLock();
            //在新会话启动时运行的代码    }    void Session_End(object sender, EventArgs e)
        {
            SqlConnection cn1 = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand("update Count set Conutzuigaolishi=Conutzuigaolishi+1",cn1);
            cn1.Open();
            cmd.ExecuteNonQuery();
            cn1.Close();
            
            
            //在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时,才会引发 Session_End 事件。如果会话模式 
            //设置为 StateServer 或 SQLServer,则不会引发该事件。    }
           
    </script>web文件中的代码
                   <system.web>
    <sessionState mode="InProc">
    </sessionState>
    然后是主页拉两个lebal控件
    pageload事件中
    protected void Page_Load(object sender, EventArgs e)
        {
            this.Label1.Text = Application["online"].ToString();
            this.Label2.Text = Application["lishi"].ToString();
        }