EFdropDownList下拉列表,ControlCache控件控制,CacheUtil缓存类

1,为了将xml文档中的数据反序列化为对象,下面这个类包含了三个常用类,和一个反序列化对象类。

(1)对象类

LeiXing
    /// <summary>
    /// 物业类型
    /// </summary>
    [Serializable]
    [XmlRoot]
    public class LeiXing
    {
        [XmlElement]
        public string Value { get; set; }

        [XmlElement]
        public string Text { get; set; }
    }

(2)EFdropDownList

EFdropDownList
    /// <summary>
    /// 输出下拉框
    /// </summary>
    public class EFdropDownList
    {
        /// <summary>
        /// 输出下拉框
        /// </summary>
        /// <typeparam name="T">下拉框列表类型</typeparam>
        /// <param name="list">下拉框</param>
        /// <param name="ControlName">输出的控件名称</param>
        /// <param name="Width">控件宽度</param>
        /// <param name="ValueName">下拉选项的value名称</param>
        /// <param name="TextName">下拉选项的text名称</param>
        /// <returns></returns>
        public static string Response<T>(List<T> list, string ControlName, int Width, string ValueName, string TextName, string CssClass, bool ReadOnly)
        {
            return Response<T>(list, ControlName, Width, ValueName, TextName, CssClass, ReadOnly, null, null, null);
        }

        /// <summary>
        /// 输出下拉框
        /// </summary>
        /// <typeparam name="T">下拉框列表类型</typeparam>
        /// <param name="list">下拉框</param>
        /// <param name="ControlName">输出的控件名称</param>
        /// <param name="Width">控件宽度</param>
        /// <param name="ValueName">下拉选项的value名称</param>
        /// <param name="TextName">下拉选项的text名称</param>
        /// <returns></returns>
        public static string Response<T>(List<T> list, string ControlName, int Width, string ValueName, string TextName, string CssClass, bool ReadOnly, string DefalutValue, string DefalutText)
        {
            return Response<T>(list, ControlName, Width, ValueName, TextName, CssClass, ReadOnly, DefalutValue, DefalutText, null);
        }

        /// <summary>
        /// 输出下拉框
        /// </summary>
        /// <typeparam name="T">下拉框列表类型</typeparam>
        /// <param name="list">下拉框</param>
        /// <param name="ControlName">输出的控件名称</param>
        /// <param name="Width">控件宽度</param>
        /// <param name="ValueName">下拉选项的value名称</param>
        /// <param name="TextName">下拉选项的text名称</param>
        /// <returns></returns>
        public static string Response<T>(List<T> list, string ControlName, int Width, string ValueName, string TextName, string CssClass, bool ReadOnly, string ControlId)
        {
            return Response<T>(list, ControlName, Width, ValueName, TextName, CssClass, ReadOnly, null, null, ControlId);
        }
        public static string Response<T>(List<T> list, string ControlName, int Width, string ValueName, string TextName, string CssClass, bool ReadOnly, string DefalutValue, string DefalutText, string ControlId)
        {
            bool iscache = true;
            if (ControlId != null && ControlId.EndsWith("_nocache"))
            {
                iscache = false;
            }
            //控件唯一标示
            string UniqueId = ControlId + ControlName + Guid.NewGuid().ToString().Replace("-", "");

            if (iscache)
            {
                object ControlObj = ControlCache.GetControl(UniqueId);
                if (ControlObj != null)//先从内存中获取
                {
                    return ControlObj.ToString();
                }
            }

            if (string.IsNullOrEmpty(ValueName))
            {
                ValueName = "Value";
            }
            if (string.IsNullOrEmpty(TextName))
            {
                TextName = "Text";
            }
            if (String.IsNullOrEmpty(ControlId))
            {
                ControlId = ControlName;
            }
            StringBuilder sb = new StringBuilder();
            string str = "";
            if (ReadOnly)
                str = "readonly=\"readonly\"";
            sb.Append("<input " + str + " style=\"width: " + Width + "px;\" class=\"reselect " + CssClass + "\" type=\"text\" name=\"" + ControlName + "\" id=\"" + ControlId + "\" />");
            sb.Append("<ul>");

            T obj = Activator.CreateInstance<T>();
            PropertyInfo[] pi = obj.GetType().GetProperties();

            int valueindex = -1;
            int textindex = -1;

            for (int j = 0; j < pi.Length; j++)
            {
                if (pi[j].Name == ValueName)
                {
                    valueindex = j;
                }
                if (pi[j].Name == TextName)
                {
                    textindex = j;
                }
            }
            if (valueindex != -1 && textindex != -1)
            {
                foreach (T t in list)
                {
                    sb.Append("<li v=\"" + pi[valueindex].GetValue(t, null) + "\">" + pi[textindex].GetValue(t, null) + "</li>");

                }
            }
            if (!string.IsNullOrEmpty(DefalutText) || !String.IsNullOrEmpty(DefalutValue))
            {
                sb.Append("<li v=\"" + DefalutValue + "\">" + DefalutText + "</li>");
            }

            sb.Append("</ul>");
            if (iscache)
            {
                ControlCache.SetControl(UniqueId, sb.ToString());
            }
            return sb.ToString();
        }

    }

(3)ControlCache

View Code
 class ControlCache
    {
        /// <summary>
        /// 保存控件
        /// </summary>
        //private static Dictionary<string, string> Controls = new Dictionary<string, string>();
        private static object lockhelper = new object();

        /// <summary>
        /// 获取控件,不存在返回Null
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public static string GetControl(string Key)
        {
            object obj = CacheUtil.GetCache(Key);
            if (obj == null)
            {
                return null;
            }
            return obj.ToString();
        }

        /// <summary>
        /// 保存控件
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="Value"></param>
        public static void SetControl(string Key, string Value)
        {
            object obj = CacheUtil.GetCache(Key);
            if (obj == null)
            {
                lock (lockhelper)
                {
                    if (obj == null)
                    {
                        CacheUtil.SetCache(Key, Value);
                    }
                }
            }
        }
    }

(4)CacheUtil

View Code
public class CacheUtil
    {
        /// <summary>
        /// 是否存在这个缓存
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        public static bool Exists(string cacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[cacheKey] != null;
        }
        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <returns></returns>
        public static object GetCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[CacheKey];
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject);
        }
        /// <summary>
        /// 移出Cache对象
        /// </summary>
        /// <param name="CacheKey"></param>
        public static void RemoveCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Remove(CacheKey);
        }
        /// <summary>
        /// 设置缓存,指定过期时间,(过期时最后一次访问缓存对象,缓存对象移除时间为0)
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <param name="objObject"></param>
        /// <param name="minutes"></param>
        public static void SetCache(string cacheKey, object objObject, int minutes)
        {
            SetCache(cacheKey, objObject, DateTime.Now.AddMinutes(minutes), TimeSpan.Zero);
        }
        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
        }

        #region 清除所有缓存
        /// <summary>
        /// 有时可能需要立即更新,这里就必须手工清除一下Cache 
        /// Cache类有一个Remove方法,但该方法需要提供一个CacheKey,但整个网站的CacheKey我们是无法得知的 
        /// 只能经过遍历 
        /// </summary>
        public static void RemoveAllCache()
        {
            System.Web.Caching.Cache cache = HttpRuntime.Cache;
            IDictionaryEnumerator cacheEnum = cache.GetEnumerator();
            ArrayList al = new ArrayList();
            while (cacheEnum.MoveNext())
            {
                al.Add(cacheEnum.Key);
            }

            foreach (string key in al)
            {
                cache.Remove(key);
            }
        }
        #endregion

        #region 显示所有缓存
        /// <summary>
        /// 显示所有缓存
        /// </summary>
        /// <returns></returns>
        public static string ShowAllCache()
        {
            string str = "";
            IDictionaryEnumerator cacheEnum = HttpRuntime.Cache.GetEnumerator();

            while (cacheEnum.MoveNext())
            {
                str += "<br />缓存名<b title=" + cacheEnum.Value.ToString() + ">[" + cacheEnum.Key + "]</b><br />";
            }
            return "当前总缓存数:" + HttpRuntime.Cache.Count + "<br />" + str;
        }
        #endregion
    }

2,下面是xml文档:

LeiXing.xml
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfLeiXing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <LeiXing>
    <Value>住宅</Value>
    <Text>住宅</Text>
  </LeiXing>
  <LeiXing>
    <Value>商铺</Value>
    <Text>商铺</Text>
  </LeiXing>
  <LeiXing>
    <Value>别墅</Value>
    <Text>别墅</Text>
  </LeiXing>
  <LeiXing>
    <Value>写字楼</Value>
    <Text>写字楼</Text>
  </LeiXing>
</ArrayOfLeiXing>

3,下面是页面:

View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.TestPage.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../js/jquery-1.4.2.min.js" type="text/javascript"></script>

    <style type="text/css">
    
    input.reselect { border: 1px #B5C2C8 solid; height: 16px; margin: 0px 2px; padding-left: 4px; padding-right: 2px; color: #0000C2; line-height: 16px; background: #fff  left top repeat-x; -moz-border-radius: 2px; -webkit-border-radius: 2px; }
   
    </style>
    
    <script type="text/javascript">
        $(function () {
            $("#username").next().hide();
            $("#username").mouseenter(function () {
                $(this).next().show();
            }).mouseleave(function () {
                $(this).next().hide();
            });


        });    
    </script>

</head>
<body>
    <form id="form1" runat="server">  

   
   <span>类型<%=WebApplication1.EFdropDownList.Response<WebApplication1.LeiXing>(WebApplication1.Common.GetLeiXingList, "username", 40, null,null, "onlydelete", false, null,"不限")%></span>

   

    <span>姓名:<input name="username" type="text" id="username" tabindex="3" /></span>
    <span>手机:<input name="userphone" type="text" id="userphone" tabindex="1" /></span>
    <span>内容:<textarea rows="auto" cols="auto"  name="content" id="content" tabindex="0"  ></textarea></span>

    </form>


 

</body>
</html>

没有达到dropdownlist的效果,js没有写好;另外样式没有设置好。

posted @ 2012-12-13 23:25  金河  阅读(239)  评论(0编辑  收藏  举报