如何从CS文件中调用类以更新或保存数据库中已过期的会话? [英] How to call a class from a cs file to update or save the session expired in database?

查看:82
本文介绍了如何从CS文件中调用类以更新或保存数据库中已过期的会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的类函数&我想使用此类在我的母版页中调用.然后,执行登录&页面的注销时间已保存到我的数据库中.

Hi, I have a class function as following & I would like to use this class to be called into my master page. Then, doing login & logout time saved of the page into my database.

namespace LogInLogOut
    {
        public partial class LogInLogOut : System.Web.UI.Page
       {        
           public static void updateLogoutTime(string username, string pcname, string module)
            {
              String connectionString = ConfigurationManager.ConnectionStrings["VSConfigConnectionString"].ConnectionString;
                SqlConnection connection = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand("UPDATE [Access]  set LogOutDate =  '" + DateTime.Today.ToString("dd/MM/yyyy") + "', LogOutTime =  '" + DateTime.Now.ToString("HH:mm:ss") + "' WHERE LoginID ='" + username + "' AND ModuleID = '" + module + "' AND comptname ='" + pcname + "' AND LogOutDate= ' '", connection);
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }
         }
    }



这是母版页中用于执行弹出消息的代码,但是我不知道要编写用于将上层类调用到我的母版页中的调用函数.希望有人可以帮助我解决这个问题.谢谢.



Here is the code in master page for doing popup message, but I have no idea to write the call function for calling the upper class into my master page. Hope anyone can assist me for this problem. Thank you.

protected void Page_Load(object sender, EventArgs e)
     {
         string csname = "timeoutWarning";
         Type cstype = this.GetType();
         if (!Page.ClientScript.IsStartupScriptRegistered(cstype, csname))
         {
             string strconfirm = "<script>" +
                 "window.setTimeout('SessionTimeOutHandler()', 10000);" +
                 "function SessionTimeOutHandler() { " +
                 "alert('Your login session is expired');" +
                 "function(){ update(document.LogInLogOut('lblUserName.Text', 'lblComputerName.Text', 'UR')); } " +
                "window.location='../login.aspx';" +
                 " } </script>";
             Page.ClientScript.RegisterStartupScript(cstype, csname, strconfirm, false);
         }
 }

推荐答案

如果您的目标是保存用户会话注销日期和时间,则意味着您可以在Global.asax文件中执行此操作,而您必须像这样写以下

If your objective is to save the user session logout date&time means you could do that in Global.asax file where you have to write like the following

void Session_End(Object sender, EventArgs E) {
   //Your method logic goes here.
}



并在web.config文件中将会话超时(默认为20分钟)设置为



and set your session timeout(default is 20 Minutes) in the web.config file as

<sessionstate timeout="10000"></sessionstate>


这篇关于如何从CS文件中调用类以更新或保存数据库中已过期的会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆