日期:2014-05-17  浏览次数:20402 次

关于设置sesson定时覆盖的问题??
如题,我十分钟获取一个值,然后把值传入sesson,把这段代码放在site.master里面,能不能实现功能?

protected void Page_Load(object sender, EventArgs e)
        {
            string qh = DateTime.Now.ToString("yyyyMMdd");
            DateTime dt1 = DateTime.Now;
            int huor = Convert.ToInt32(dt1.Hour.ToString());
            int minute = Convert.ToInt32(dt1.Minute.ToString());
            int n = (huor * 60 + minute) / 10;
            string n3 = n.ToString();
            string qishu;
            if (n3.Length < 3)
            {
                qishu = qh + "0" + n3;
            }
            else
            {
                qishu = qh + n3;
            }
            Session["qishu"] = qishu;
        }

------解决方案--------------------
这种情况应该在global.asax的Application_BeginRequest事件中比较合适,你上面的代码问题多多,比如这个:int huor = Convert.ToInt32(dt1.Hour.ToString()); dt1.Hour本来就是int类型,何必把它转换成string然后再用Convert.ToInt32转成int??而且你判断是否间隔了十分钟的代码实在是看不懂。
------解决方案--------------------

 public class Global : System.Web.HttpApplication
    {
        public static Timer gtimer = null;
        void Application_Start(object sender, EventArgs e)
        {
            gtimer = new Timer(300000);
         &n