if(IsPostBack)
  return;
else 
  this.Label1.Text = this.Session[index]
最好把读出来的数据放到Session中,然后通过IsPostBack来确定是否是第二次打开

解决方案 »

  1.   

    错了,
    if(IsPostBack)
     this.Label1.Text = this.Session[index]
    else
      读数据库
      

  2.   

    首先,适合做缓存的是更新次数少读取次数多的数据,譬如广告啥的。
    其次,缓存是给多个用户用比较合适。
    如果给一个用户用,除非这一个用户跟服务器的交互特别频繁。
    所以个人建议不适合用sessionif (Cache["KeyName"]==null)  //从数据库Load数据;
    {
        Cache["KeyName"] = db.LoadDataTable(sqlString);  //这最好弄个DataAccess调用
    }if (Cache["KeyName"]!=null)  //万一从数据库Load失败
    {
        DataTable dt = (DataTable)Cache["KeyName"];
        //然后从DataTable中select你需要的数据返回
        //return ....
    }当然ds ,dt啥的都可以放cache里,就看你的需求了
      

  3.   

    string strWhere = "";
            string key = basicKey + "List_All";
            if (HttpRuntime.Cache[key] != null)
                return (DataTable)HttpRuntime.Cache[key];
            else
            {
            if (UnitType != "" && UnitType != null)
            {
                strWhere += "and type like '%" + UnitType + "%'";
            }
            if (unit_id != "" && unit_id != null)
            {
                strWhere += "and unit_id='" + unit_id + "'";
            }
            if (branch_id != "" && branch_id != null)
            {
                strWhere += "and branch_id='" + branch_id + "'";
            }        if (peotype != "" && peotype != null)
            {
                strWhere += "and peotype like '%" + peotype + "%'";
            }
            if (name != "" && name != null)
            {
                strWhere += "and name like '%" + name + "%'";
            }
            if (idcard != "" && idcard != null)
            {
                strWhere += "and idcard like '%" + idcard + "%'";
            }
            if (flag != "" && flag != null)
            {
                strWhere += "and flag like '%" + flag + "%'";
            }
            if (sex != "" && sex != null)
            {
                strWhere += "and sex like '%" + sex + "%'";
            }
            if (xzqh == "230000")
            {
                strWhere += "and xzqh like '%2300%'";
            }
            else
            {
                strWhere += " and xzqh='" + xzqh + "'";
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 5000* from View_PeopleSearche");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere.Substring(4, strWhere.Length - 4));
            }
            dt = sqldata.GetTable(strSql.ToString());
            Caches.TryAddCache(key, dt, System.Web.Caching.CacheItemPriority.Normal);
            return dt;