星星评分控件----------WebForm服务器控件开发系列

该控件是继承于 WebControl 基类开发的。目前初步成型,现在的版本只能给大家作为一个基础自定义控件开发的参考,不建议你直接使用,因为还没有进行内存泄漏测试和大量的其他测试。这是个服务器控件不同于客户端的 js控件 和可 WinForm 控件,因为这些客户端控件只是在客户端本地使用,就算是出现内存泄漏,一般重启客户端程序就可以了。但Web服务器控件是运行在服务器上的,而且是大量用户并发使用的,就算有一个小小的内存泄漏,在大量客户端请求下会造成很严重后果。

之所以把它做成Web服务器全局刷新控件,因为JQuery 插件网络上已经很多了而且很完善。而且原生的Web服务器控件很少功能很简单。那有些人问为什么不做成Ajax异步刷新的服务器控件。因为我反编译过 DevExpress Web控件库(全局刷新服务器控件库)和 EXT.NET  Web控件库(Ajax异步刷新服务器控件库) 粗略的看了下他们的开发逻辑,做成Ajax异步这种会更更复杂。所以目前选定全局刷新这种。

开发服务器控件主要是有 ViewState管理  、 复杂属性 、 JS脚本管理 、 TypeConverter数据类型转换器 这些知识。 

该控件使用了图标字体,因为现在Web开发图标字体比较常见,也是一种比较好的选择。为了能在在 VS 的页面设计器能能浏览到图标字体效果,这里对控件做了些特殊处理,系统必须安装项目里面的 wcle_ico.tff 字体。因为 VS 的页面设计器无法直接预览到css加载的图标字体,这时候控件改用1系统安装自定义图标字体输出来模拟。

  1     /// <summary>
  2     /// 图标工具类
  3     /// </summary>
  4     [Description("图标工具类")]
  5     public static class IcoUtility
  6     {
  7         #region
  8 
  9         /// <summary>
 10         /// 图标字体文件名称
 11         /// </summary>
 12         [Description("图标字体文件名称")]
 13         public const string IcoFontFileName = "wcle_ico";
 14 
 15         private static object icolist_obj = new object();
 16         private static IcoFontItemCollection allIco;
 17         /// <summary>
 18         /// 所有图标字体信息
 19         /// </summary>
 20         [Description("所有图标字体信息")]
 21         public static IcoFontItemCollection AllIco
 22         {
 23             get
 24             {
 25                 if (IcoUtility.allIco == null)
 26                 {
 27                     lock (IcoUtility.icolist_obj)
 28                     {
 29                         IcoUtility.allIco = new IcoFontItemCollection();
 30 
 31                         Type t = typeof(Ico);
 32                         FieldInfo[] fieldArr = t.GetFields();
 33                         for (int i = 0; i < fieldArr.Length; i++)
 34                         {
 35                             IcoUnicodeAttribute[] ico_attr = (IcoUnicodeAttribute[])fieldArr[i].GetCustomAttributes(typeof(IcoUnicodeAttribute), false);
 36                             if (ico_attr != null && ico_attr.Length > 0)
 37                             {
 38                                 IcoUnicodeAttribute unicode = ico_attr[0];
 39                                 IcoFontItem icoFontInfo = new IcoFontItem() { Name = unicode.Name, Unicode = unicode.Unicode, WinUnicode = unicode.WinUnicode, WebUnicode = unicode.WebUnicode };
 40                                 IcoUtility.allIco.Add(icoFontInfo);
 41                             }
 42                         }
 43                     }
 44                 }
 45 
 46                 return allIco;
 47             }
 48         }
 49 
 50         #endregion
 51 
 52         #region 公开方法
 53 
 54         /// <summary>
 55         /// 根据图标名称获取图标Unicode编码(Web用 &#x开头)
 56         /// </summary>
 57         /// <param name="Ico"></param>
 58         /// <returns></returns>
 59         public static string GetWebUnicode(Ico Ico)
 60         {
 61             return IcoUtility.GetWebUnicode(Ico.ToString());
 62         }
 63 
 64         /// <summary>
 65         /// 根据图标名称获取图标Unicode编码(Web用 &#x开头)
 66         /// </summary>
 67         /// <param name="Name"></param>
 68         /// <returns></returns>
 69         public static string GetWebUnicode(string Name)
 70         {
 71             Name = Name.Trim();
 72             foreach (IcoFontItem ico in IcoUtility.AllIco)
 73             {
 74                 if (ico.Name == Name)
 75                 {
 76                     return ico.WebUnicode;
 77                 }
 78             }
 79             return "";
 80         }
 81 
 82         /// <summary>
 83         /// 根据图标名称获取图标Unicode编码(Win用)
 84         /// </summary>
 85         /// <param name="Ico"></param>
 86         /// <returns></returns>
 87         public static string GetWinUnicode(Ico Ico)
 88         {
 89             return IcoUtility.GetWinUnicode(Ico.ToString());
 90         }
 91 
 92         /// <summary>
 93         /// 根据图标名称获取图标Unicode编码(Win用)
 94         /// </summary>
 95         /// <param name="Name"></param>
 96         /// <returns></returns>
 97         public static string GetWinUnicode(string Name)
 98         {
 99             Name = Name.Trim();
100             foreach (IcoFontItem ico in IcoUtility.AllIco)
101             {
102                 if (ico.Name == Name)
103                 {
104                     return ico.WinUnicode;
105                 }
106             }
107             return "";
108         }
109 
110         /// <summary>
111         /// 获取图标字符编码信息
112         /// </summary>
113         /// <param name="WinUnicode">图标</param>
114         /// <returns></returns>
115         public static IcoFontItem GetIcoFontItem(Ico Ico)
116         {
117             foreach (IcoFontItem item in IcoUtility.AllIco)
118             {
119                 if (item.Ico == Ico)
120                 {
121                     return item;
122                 }
123             }
124             return null;
125         }
126 
127         /// <summary>
128         /// 获取图标字符编码信息
129         /// </summary>
130         /// <param name="WinUnicode">图标名称</param>
131         /// <returns></returns>
132         public static IcoFontItem GetIcoFontItemByName(string Name)
133         {
134             Name = Name.Trim();
135             foreach (IcoFontItem item in IcoUtility.AllIco)
136             {
137                 if (item.Name == Name)
138                 {
139                     return item;
140                 }
141             }
142             return null;
143         }
144 
145         /// <summary>
146         /// 获取图标字符编码信息
147         /// </summary>
148         /// <param name="WinUnicode">\u 开头四位图标Unicode编码</param>
149         /// <returns></returns>
150         public static IcoFontItem GetIcoFontItemByWinUnicode(string WinUnicode)
151         {
152             WinUnicode = WinUnicode.Trim();
153             foreach (IcoFontItem item in IcoUtility.AllIco)
154             {
155                 if (item.WinUnicode == WinUnicode)
156                 {
157                     return item;
158                 }
159             }
160             return null;
161         }
162 
163         /// <summary>
164         /// 获取图标字符编码信息
165         /// </summary>
166         /// <param name="WinUnicode"> &#x 开头四位图标Unicode编码</param>
167         /// <returns></returns>
168         public static IcoFontItem GetIcoFontItemByWebUnicode(string WebUnicode)
169         {
170             WebUnicode = WebUnicode.Trim();
171             foreach (IcoFontItem item in IcoUtility.AllIco)
172             {
173                 if (item.WebUnicode == WebUnicode)
174                 {
175                     return item;
176                 }
177             }
178             return null;
179         }
180 
181         #endregion
182     }
183 
184     /// <summary>
185     /// 图标 通过https://icomoon.io 网站生产
186     /// </summary>
187     [Description("图标")]
188     public enum Ico
189     {
190         /// <summary>
191         /// 星星
192         /// </summary>
193         [Description("星星")]
194         [IcoUnicodeAttribute("wcle_ico_ratingstars", "1111", "\u1111", "&#x1111")]
195         wcle_ico_ratingstars,
196         /// <summary>
197         /// 交叉
198         /// </summary>
199         [Description("交叉")]
200         [IcoUnicodeAttribute("wcle_ico_ratingclear", "1112", "\u1112", "&#x1112")]
201         wcle_ico_ratingclear,
202         /// <summary>
203         /// 等级
204         /// </summary>
205         [Description("等级")]
206         [IcoUnicodeAttribute("wcle_ico_level", "1113", "\u1113", "&#x1113")]
207         wcle_ico_level,
208 
209         /// <summary>
210         /// 书签
211         /// </summary>
212         [Description("书签")]
213         [IcoUnicodeAttribute("wcle_ico_bookmark", "1114", "\u1114", "&#x1114")]
214         wcle_ico_bookmark,
215         /// <summary>
216         /// 黄冠
217         /// </summary>
218         [Description("黄冠")]
219         [IcoUnicodeAttribute("wcle_ico_crown", "1115", "\u1115", "&#x1115")]
220         wcle_ico_crown,
221         /// <summary>
222         /// 书签2
223         /// </summary>
224         [Description("书签2")]
225         [IcoUnicodeAttribute("wcle_ico_bookmark2", "1116", "\u1116", "&#x1116")]
226         wcle_ico_bookmark2,
227         /// <summary>
228         /// 心型
229         /// </summary>
230         [Description("心型")]
231         [IcoUnicodeAttribute("wcle_ico_heart", "1117", "\u1117", "&#x1117")]
232         wcle_ico_heart,
233         /// <summary>
234         /// 钻石
235         /// </summary>
236         [Description("钻石")]
237         [IcoUnicodeAttribute("wcle_ico_diamond", "1118", "\u1118", "&#x1118")]
238         wcle_ico_diamond,
239         /// <summary>
240         /// 月亮
241         /// </summary>
242         [Description("月亮")]
243         [IcoUnicodeAttribute("wcle_ico_moon", "1119", "\u1119", "&#x1119")]
244         wcle_ico_moon
245     }
246 
247     /// <summary>
248     ///图标字符编码信息集合
249     /// </summary>
250     [Description("图标字符编码信息集合")]
251     public sealed class IcoFontItemCollection : IList, ICollection, IEnumerable
252     {
253         private ArrayList icoFontItemList = new ArrayList();
254 
255         public IcoFontItemCollection()
256         {
257 
258         }
259 
260         #region IEnumerable
261 
262         public IEnumerator GetEnumerator()
263         {
264             IcoFontItem[] listArray = new IcoFontItem[this.icoFontItemList.Count];
265             for (int index = 0; index < listArray.Length; ++index)
266             {
267                 listArray[index] = (IcoFontItem)this.icoFontItemList[index];
268             }
269             return listArray.GetEnumerator();
270         }
271 
272         #endregion
273 
274         #region ICollection
275 
276         public void CopyTo(Array array, int index)
277         {
278             for (int i = 0; i < this.Count; i++)
279             {
280                 array.SetValue(this.icoFontItemList[i], i + index);
281             }
282         }
283 
284         public int Count
285         {
286             get
287             {
288                 return this.icoFontItemList.Count;
289             }
290         }
291 
292         public bool IsSynchronized
293         {
294             get
295             {
296                 return false;
297             }
298         }
299 
300         public object SyncRoot
301         {
302             get
303             {
304                 return (object)this;
305             }
306         }
307 
308         #endregion
309 
310         #region IList
311 
312         public int Add(object value)
313         {
314             if (!(value is IcoFontItem))
315             {
316                 throw new ArgumentException("IcoFontItem");
317             }
318             return this.Add((IcoFontItem)value);
319         }
320 
321         public int Add(IcoFontItem item)
322         {
323             if (item == null)
324             {
325                 throw new ArgumentNullException("item");
326             }
327             this.icoFontItemList.Add(item);
328             return this.Count - 1;
329         }
330 
331         public void Clear()
332         {
333             this.icoFontItemList.Clear();
334         }
335 
336         public bool Contains(object value)
337         {
338             if (value == null)
339                 throw new ArgumentNullException("value");
340             return this.IndexOf(value) != -1;
341         }
342 
343         bool IList.Contains(object item)
344         {
345             if (item is IcoFontItem)
346             {
347                 return this.Contains((IcoFontItem)item);
348             }
349             return false;
350         }
351 
352         public int IndexOf(object item)
353         {
354             if (item is IcoFontItem)
355             {
356                 return this.icoFontItemList.IndexOf(item);
357             }
358             return -1;
359         }
360 
361         public void Insert(int index, object value)
362         {
363             throw new NotImplementedException();
364         }
365 
366         public bool IsFixedSize
367         {
368             get { return false; }
369         }
370 
371         public bool IsReadOnly
372         {
373             get { return false; }
374         }
375 
376         public void Remove(object value)
377         {
378             throw new NotImplementedException();
379         }
380 
381         public void Remove(IcoFontItem item)
382         {
383             throw new NotImplementedException();
384         }
385 
386         public void RemoveAt(int index)
387         {
388             throw new NotImplementedException();
389         }
390 
391         /// <summary>
392         /// 
393         /// </summary>
394         /// <param name="Ico">图标</param>
395         /// <returns></returns>
396         public IcoFontItem this[Ico Ico]
397         {
398             get
399             {
400                 foreach (IcoFontItem item in this.icoFontItemList)
401                 {
402                     if (item.Ico == Ico)
403                         return item;
404                 }
405                 return null;
406             }
407         }
408 
409         /// <summary>
410         /// 
411         /// </summary>
412         /// <param name="Name">图标名称</param>
413         /// <returns></returns>
414         public IcoFontItem this[string Name]
415         {
416             get
417             {
418                 foreach (IcoFontItem item in this.icoFontItemList)
419                 {
420                     if (item.Name == Name)
421                         return item;
422                 }
423                 return null;
424             }
425         }
426 
427         /// <summary>
428         /// 
429         /// </summary>
430         /// <param name="index">索引</param>
431         /// <returns></returns>
432         public IcoFontItem this[int index]
433         {
434             get
435             {
436                 return (IcoFontItem)this.icoFontItemList[index];
437             }
438             set
439             {
440                 this.icoFontItemList[index] = (IcoFontItem)value;
441             }
442         }
443 
444         object IList.this[int index]
445         {
446             get
447             {
448                 return (object)this.icoFontItemList[index];
449             }
450             set
451             {
452                 this.icoFontItemList[index] = (IcoFontItem)value;
453             }
454         }
455 
456         #endregion
457     }
458 
459     /// <summary>
460     /// 图标字符编码信息
461     /// </summary>
462     [Description("图标字符编码信息")]
463     public class IcoFontItem
464     {
465         /// <summary>
466         /// 图标
467         /// </summary>
468         public Ico Ico { get; set; }
469         /// <summary>
470         /// 图标名称
471         /// </summary>
472         public string Name { get; set; }
473         /// <summary>
474         /// 四位图标Unicode编码
475         /// </summary>
476         public string Unicode { get; set; }
477         /// <summary>
478         /// \u 开头四位图标Unicode编码(Win用)
479         /// </summary>
480         public string WinUnicode { get; set; }
481         /// <summary>
482         /// &#x 开头四位图标Unicode编码(Web用)
483         /// </summary>
484         public string WebUnicode { get; set; }
485     }
486 
487     /// <summary>
488     /// 图标字体对应的四位Unicode编码
489     /// </summary>
490     [Description("图标字体对应的四位Unicode编码")]
491     [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
492     public class IcoUnicodeAttribute : Attribute
493     {
494         /// <summary>
495         /// 图标名称
496         /// </summary>
497         public string Name { get; set; }
498 
499         /// <summary>
500         /// 四位图标Unicode编码
501         /// </summary>
502         public string Unicode { get; set; }
503 
504         /// <summary>
505         /// \u 开头四位图标Unicode编码(Win用)
506         /// </summary>
507         public string WinUnicode { get; set; }
508 
509         /// <summary>
510         /// &#x 开头四位图标Unicode编码(Web用)
511         /// </summary>
512         public string WebUnicode { get; set; }
513 
514         public IcoUnicodeAttribute(string Name, string Unicode, string WinUnicode, string WebUnicode)
515         {
516             this.Name = Name;
517             this.Unicode = Unicode;
518             this.WinUnicode = WinUnicode;
519             this.WebUnicode = WebUnicode;
520         }
521 
522     }
图标字体管理类

 1 wclej = {
 2     ChangeGradeValue: function (owner, id, callback) {
 3         /// <summary>更改等级</summary>
 4         /// <param name="obj" type="string">当前点击节点</param>
 5         /// <param name="id" type="string">服务器控件uniqueid</param>
 6         /// <param name="callback" type="bool">是否回发</param>
 7 
 8         var gradevalue = document.getElementById(id + '_value');
 9         var currentvalue = owner.getAttribute('grade');
10         if (gradevalue == undefined || currentvalue == undefined)
11             return;
12 
13         gradevalue.value = currentvalue;
14 
15         if (callback) {
16             setTimeout("__doPostBack('" + id + "','')", 0);
17         }
18         else {
19             var ui = document.getElementById(id + '_ui');
20             var sumgradecolor = ui.getAttribute('sumgradecolor');
21             var currentgradecolor = ui.getAttribute('currentgradecolor');
22 
23             var gradeArr = new Array();
24             var index = -1;
25             for (var i = 0; i < ui.rows[0].cells.length; i++) {
26                 var itemtype = ui.rows[0].cells[i].getAttribute('itemtype');
27                 if (itemtype != undefined && itemtype == 'grade') {
28                     index++;
29                     gradeArr[index] = ui.rows[0].cells[i].children[0];
30                 }
31             }
32 
33             for (var i = 0; i < gradeArr.length; i++) {
34                 gradeArr[i].style.color = (i < currentvalue ? currentgradecolor : sumgradecolor)
35             }
36         }
37     },
38     SetGradeValue: function (currentvalue, id, callback) {
39         /// <summary>设置等级</summary>
40         /// <param name="currentvalue" type="int">要设置的等级</param>
41         /// <param name="id" type="string">服务器控件uniqueid</param>
42         /// <param name="callback" type="bool">是否回发</param>
43 
44         var gradevalue = document.getElementById(id + '_value');
45         if (gradevalue == undefined || currentvalue == undefined)
46             return;
47 
48         gradevalue.value = currentvalue;
49 
50         if (callback) {
51             setTimeout("__doPostBack('" + id + "','')", 0);
52         }
53         else {
54             var ui = document.getElementById(id + '_ui');
55             var sumgradecolor = ui.getAttribute('sumgradecolor');
56             var currentgradecolor = ui.getAttribute('currentgradecolor');
57 
58             var gradeArr = new Array();
59             var index = -1;
60             for (var i = 0; i < ui.rows[0].cells.length; i++) {
61                 var itemtype = ui.rows[0].cells[i].getAttribute('itemtype');
62                 if (itemtype != undefined && itemtype == 'grade') {
63                     index++;
64                     gradeArr[index] = ui.rows[0].cells[i].children[0];
65                 }
66             }
67 
68             for (var i = 0; i < gradeArr.length; i++) {
69                 gradeArr[i].style.color = (i < currentvalue ? currentgradecolor : sumgradecolor)
70             }
71         }
72     },
73     GetGradeValue: function (id) {
74         /// <summary>获取等级</summary>
75         /// <param name="id" type="string">服务器控件uniqueid</param>
76         /// <return name="callback" type="bool">等级</return>
77 
78         var gradevalue = document.getElementById(id + '_value');
79         if (gradevalue == undefined)
80             return -1;
81 
82         return gradevalue.value;
83     }
84 };
控件js代码

 

源码下载:星星评分控件.zip

posted @ 2020-09-09 15:41  唧唧复唧唧木兰当户织  阅读(2266)  评论(1编辑  收藏  举报