asp.net notes

  1. cache's expire problem
     string sendTimeKey = "abc_sendtime";
            //DateTime dtLast = (DateTime)cache[sendTimeKey];
            if (Cache[sendTimeKey] == null)
            {
                Cache.Insert(sendTimeKey, 1, null, DateTime.MaxValue, TimeSpan.FromSeconds(3));//, CacheItemPriority.Normal, null);
                Response.Write("no cache, added");
            }
            else
            {
                Cache[sendTimeKey] = (int)Cache[sendTimeKey] + 1;// .Insert(sendTimeKey, DateTime.Now, null, DateTime.MaxValue, TimeSpan.FromSeconds(50));
                //if (total >= 100)
                {
                    Response.Write("has cache");
                }
            }
            Response.Write(Cache[sendTimeKey].ToString());
    上面的代码会造成一旦进入else语句之后,cache将不会在3秒内过期,除非应用重启,原因是直接cache[key] = value,并未设置cache的过期时间,因此将永不过期,因此如果要让cache在sliding expiration,则应该在else中也使用cache.insert()指定过期时间.
    当然,如果不使用slidingExpiration,而是使用绝对时间过期,则只需要在第4个参数不用dateTime.MaxValue,设置一个相应的时间即可,而最后一个参数则应该为TimeSpan.Zero.
  2.  *.axd文件在http://ip/或机器名方式下访问不了,而在vs2k8的debug模式下可以看到,最后发现是在IIS中需要将application pool设置为classic .net pool,而不是defaultpool即可.
  3. dropdownlist中的value如果有相同的,则可能在选择后面的ListItem时,selectedIndex仍然是指向前面的;另外就是应该在初始化dropdownlist时判断Page.IsPostBack.
  4.  关于使用response.Redirect或response.WriteFile时的一点解决方案
    使用response.Redirect或response.writeFile下载文件时,如果还要对当前页面中的server control进行处理,
    则在处理完所有页面逻辑代码之后,使用js代码重定向到下载文件,否则与UI相关的代码将不会执行.
    ex:
    btnDown_click()
    {
    lbAll.Visible = true;  // 显示另一个linkbutton
            string strStartUpScript = "<script language='javascript' type='text/javascript'>window.location.href='newdown3.aspx?fn=" + Server.UrlEncode(fileName) + "';</script>";
            ClientScript.RegisterStartupScript(Page.GetType(), "PopupScript", strStartUpScript);
    }


posted @ 2008-01-19 21:43  margiex  阅读(292)  评论(0编辑  收藏  举报