也算是自己刚毕业时候的作品吧,当时在一家创业公司工作,考虑给公司建立一个公司的控件库,于是也学习过一段时间的控件设计,当时领我入门的是《道不远人》那本控件开发书,觉得谭振宁写的还是相当不错的,推荐一下:)。那么就开始分享我设计的一些控件,并且告诉一些基本的设计思路。
具体实现了这些控件:
CheckTextBox: 可以对于输入TextBox的字符做出判断
HighSlideHyperLink: 点击HyperLink可以弹出相关Div层或者Img图象
ContextMenu: 上下文菜单特效
Menu: 菜单特效
EnablePageDataList: 具有分页功能的DataList
EnablePageRepeater: 具有分页功能的Repeater
GroupDropDownList: 具有分组选项功能的DropDownList
GroupListBox: 具有分组选项功能的ListBox
SearchCloud:Tag云图,可以根据关键词的搜索数量,进行排列
1. CheckTextBox
效果图:
该控件继承自TextBox控件:
[DefaultProperty( "Text" )] |
2 |
[ToolboxData( "<{0}:CheckTextBox runat=server>" )] |
3 |
public class CheckTextBox : TextBox |
4 |
{ |
5 |
... |
6 |
} |
控件的属性代码:
private TextType _inputType; |
02 |
[Description( @"文本框输入的类型: |
03 |
String------任意文本, |
04 |
Upper---------大写, |
05 |
Lower---------小写, |
06 |
Number--------任意数字, |
07 |
FormatNumber--格式化指定的小数, |
08 |
Date----------日期, |
09 |
ChineseLanguage-中文, |
10 |
IP------------IP地址, |
11 |
Phone---------固定电话 |
12 |
" ) |
13 |
] |
14 |
public TextType InputType |
15 |
{ |
16 |
get |
17 |
{ |
18 |
return _inputType; |
19 |
} |
20 |
set |
21 |
{ |
22 |
if ((value < TextType.String) || (value > TextType.Phone)) |
23 |
{ |
24 |
throw new ArgumentOutOfRangeException( "无效属性值" ); |
25 |
} |
26 |
_inputType = value; |
27 |
} |
28 |
} |
Description属性将在设计窗口属性中显示出描述:
通过AddAttributesToRender方法实现脚本的注册:
protected override void AddAttributesToRender(HtmlTextWriter writer) |
2 |
{ |
3 |
... |
4 |
} |
其中InputType包括几种方式以及一些事件发生的脚本:
public static class TextScript |
02 |
{ |
03 |
public static string ChineseLanguage = @"/^[\u4E00-\u9FA5]*$/" ; |
04 |
public static string Lower = @"/^[a-z]*$/" ; |
05 |
public static string Number = @"/^[0-9]*$/" ; |
06 |
public static string Phone = @"/^\d{1,4}([-\/](\d{1,8}?)?)?$/" ; |
07 |
public static string IP = @"/^\d{1,3}([.\/](\d{1,3}([.\/](\d{1,3}([.\/](\d{1,3}([.\/](\d{1,3}([.\/](\d{1,3}?)?)?)?)?)?)?)?)?)?)?$/" ; |
08 |
public static string Upper = @"/^[A-Z]*$/" ; |
09 |
public static string OnKeyPress = @"return regInput(this, {0},String.fromCharCode(event.keyCode))" ; |
10 |
public static string OnPaste = @"return regInput(this,{0},window.clipboardData.getData('Text'))" ; |
11 |
public static string OnDrop = @"return regInput(this,{0},event.dataTransfer.getData('Text'))" ; |
12 |
public static string OnFocusCloseInput = "this.style.imeMode='disabled'" ; |
13 |
} |
调用服务器控件页面代码:
< div > |
2 |
<? xml:namespace prefix = cc1 />< cc1:checktextbox id = "CheckTextBox1" cssclass = "txtblur" onfocuscss = "txtfocus" onblurcss = "txtblur" isdisplaytime = "False" inputtype = "Number" runat = "server" ></ cc1:checktextbox > |
3 |
< br > |
4 |
< cc1:checktextbox id = "CheckTextBox2" cssclass = "txtblur" isdisplaytime = "False" inputtype = "Date" runat = "server" ></ cc1:checktextbox >< br > |
5 |
< cc1:checktextbox id = "CheckTextBox3" cssclass = "txtblur" onfocuscss = "txtfocus" onblurcss = "txtblur" inputtype = "Date" runat = "server" ></ cc1:checktextbox >< br > |
6 |
< cc1:checktextbox id = "CheckTextBox4" cssclass = "txtblur" onfocuscss = "txtfocus" onblurcss = "txtblur" inputtype = "FormatNumber" runat = "server" floatlength = "4" ></ cc1:checktextbox >< br > |
7 |
< cc1:checktextbox id = "CheckTextBox5" cssclass = "txtblur" onfocuscss = "txtfocus" onblurcss = "txtblur" inputtype = "Phone" runat = "server" floatlength = "4" showmessage = "请输入正确的电话号码" ></ cc1:checktextbox > |
8 |
< br > |
9 |
< cc1:checktextbox id = "CheckTextBox6" language = "Chinese" isdisplaytime = "False" inputtype = "Date" runat = "server" ></ cc1:checktextbox ></ div > |
2. HighSlideHyperLink
效果图:
看到类的头部:
1 |
[assembly: WebResource( "Conovosoft.Web.UI.WebControls.highslide.css" , "text/css" )] |
2 |
[assembly: WebResource( "Conovosoft.Web.UI.WebControls.highslide.js" , "text/javascript" )] |
3 |
[assembly: WebResource( "Conovosoft.Web.UI.WebControls.highslide-html.js" , "text/javascript" )] |
这里控件需要引用一些JS以及CSS的资源文件,highslide作为第三方的JS脚本,为了实现图片放大的效果。
在OnPreRender方法中注册脚本文件:
protected override void OnPreRender(EventArgs e) |
02 |
{ |
03 |
RegisterClientCSSResource( "Conovosoft.Web.UI.WebControls.highslide.css" ); |
04 |
Page.ClientScript.RegisterClientScriptResource( this .GetType(), |
05 |
"Conovosoft.Web.UI.WebControls.highslide.js" ); |
06 |
Page.ClientScript.RegisterClientScriptResource( this .GetType(), |
07 |
"Conovosoft.Web.UI.WebControls.highslide-html.js" ); |
08 |
if (!Page.ClientScript.IsStartupScriptRegistered( "baseTextScript" )) |
09 |
{ |
10 |
if ( this .ControlType == InputType.Div) |
11 |
{ |
12 |
if (! string .IsNullOrEmpty( this .DivID)) |
13 |
{ |
14 |
this .Page.ClientScript.RegisterStartupScript( this .GetType(), "baseTextScript" , @"<script type=" text/javascript"> |
15 |
document.getElementById( '" + this.DivID + @"' ).className= 'highslide-html-content' ;</script>", false ); |
16 |
} |
17 |
} |
18 |
if ( this .ControlType == InputType.Img) |
19 |
{ |
20 |
if (! string .IsNullOrEmpty( this .CaptionID)) |
21 |
{ |
22 |
this .Page.ClientScript.RegisterStartupScript( this .GetType(), "baseTextScript" , @"<script type=" text/javascript"> |
23 |
document.getElementById( '" + this.CaptionID + @"' ).className= 'highslide-caption' ;</script>", false ); |
24 |
} |
25 |
} |
26 |
} |
27 |
base .OnPreRender(e); |
28 |
} |
调用代码:
3. EnablePageRepeater
效果图:
using System; |
002 |
using System.Collections.Generic; |
003 |
using System.Text; |
004 |
using System.Web.UI.WebControls; |
005 |
using System.ComponentModel; |
006 |
using System.Collections; |
007 |
008 |
namespace Conovosoft.Web.UI.WebControls |
009 |
{ |
010 |
public class EnablePageRepeater : Repeater |
011 |
{ |
012 |
private static readonly object EventPageIndexChanged = new object (); |
013 |
PagedDataSource pagedDataSource; |
014 |
015 |
[DefaultValue( false ), Category( "Paging" )] |
016 |
public virtual bool AllowPaging |
017 |
{ |
018 |
get |
019 |
{ |
020 |
object obj2 = this .ViewState[ "AllowPaging" ]; |
021 |
if (obj2 != null ) |
022 |
{ |
023 |
return ( bool )obj2; |
024 |
} |
025 |
return false ; |
026 |
} |
027 |
set |
028 |
{ |
029 |
this .ViewState[ "AllowPaging" ] = value; |
030 |
} |
031 |
} |
032 |
033 |
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), |
034 |
Browsable( false )] |
035 |
public virtual int VirtualItemCount |
036 |
{ |
037 |
get |
038 |
{ |
039 |
object obj2 = this .ViewState[ "VirtualItemCount" ]; |
040 |
if (obj2 != null ) |
041 |
{ |
042 |
return ( int )obj2; |
043 |
} |
044 |
return 0; |
045 |
} |
046 |
set |
047 |
{ |
048 |
if (value < 0) |
049 |
{ |
050 |
throw new ArgumentOutOfRangeException( "value" ); |
051 |
} |
052 |
this .ViewState[ "VirtualItemCount" ] = value; |
053 |
} |
054 |
} |
055 |
056 |
[DefaultValue(10), Category( "Paging" )] |
057 |
public virtual int PageSize |
058 |
{ |
059 |
get |
060 |
{ |
061 |
object obj2 = this .ViewState[ "PageSize" ]; |
062 |
if (obj2 != null ) |
063 |
{ |
064 |
return ( int )obj2; |
065 |
} |
066 |
return 10; |
067 |
} |
068 |
set |
069 |
{ |
070 |
if (value < 1) |
071 |
{ |
072 |
throw new ArgumentOutOfRangeException( "value" ); |
073 |
} |
074 |
this .ViewState[ "PageSize" ] = value; |
075 |
} |
076 |
} |
077 |
078 |
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), |
079 |
Browsable( false )] |
080 |
public int PageCount |
081 |
{ |
082 |
get |
083 |
{ |
084 |
if ( this .pagedDataSource != null ) |
085 |
{ |
086 |
return this .pagedDataSource.PageCount; |
087 |
} |
088 |
object obj2 = this .ViewState[ "PageCount" ]; |
089 |
if (obj2 == null ) |
090 |
{ |
091 |
return 0; |
092 |
} |
093 |
return ( int )obj2; |
094 |
} |
095 |
} |
096 |
097 |
[Browsable( false ), |
098 |
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] |
099 |
public int CurrentPageIndex |
100 |
{ |
101 |
get |
102 |
{ |
103 |
object obj2 = this .ViewState[ "CurrentPageIndex" ]; |
104 |
if (obj2 != null ) |
105 |
{ |
106 |
return ( int )obj2; |
107 |
} |
108 |
return 0; |
109 |
} |
110 |
set |
111 |
{ |
112 |
if (value < 0) |
113 |
{ |
114 |
throw new ArgumentOutOfRangeException( "value" ); |
115 |
} |
116 |
this .ViewState[ "CurrentPageIndex" ] = value; |
117 |
OnPageIndexChanged( new DataGridPageChangedEventArgs( this , value)); |
118 |
} |
119 |
} |
120 |
121 |
protected virtual void OnPageIndexChanged(DataGridPageChangedEventArgs e) |
122 |
{ |
123 |
DataGridPageChangedEventHandler handler = (DataGridPageChangedEventHandler)Events[EventPageIndexChanged]; |
124 |
if (handler != null ) |
125 |
handler( this , e); |
126 |
} |
127 |
128 |
[Category( "Action" )] |
129 |
public event DataGridPageChangedEventHandler PageIndexChanged |
130 |
{ |
131 |
add |
132 |
{ |
133 |
base .Events.AddHandler(EventPageIndexChanged, value); |
134 |
} |
135 |
remove |
136 |
{ |
137 |
base .Events.RemoveHandler(EventPageIndexChanged, value); |
138 |
} |
139 |
} |
140 |
141 |
protected override System.Collections.IEnumerable GetData() |
142 |
{ |
143 |
IEnumerable dataSource = base .GetData(); |
144 |
if (AllowPaging && dataSource != null ) |
145 |
{ |
146 |
pagedDataSource = new PagedDataSource(); |
147 |
ICollection collection = dataSource as ICollection; |
148 |
if (collection != null ) |
149 |
{ |
150 |
pagedDataSource.VirtualCount = collection.Count; |
151 |
VirtualItemCount = pagedDataSource.VirtualCount; |
152 |
153 |
} |
154 |
else if (dataSource is IListSource) |
155 |
{ |
156 |
pagedDataSource.VirtualCount = (dataSource as IListSource).GetList().Count; |
157 |
VirtualItemCount = pagedDataSource.VirtualCount; |
158 |
} |
159 |
else if (VirtualItemCount > 0) |
160 |
{ |
161 |
pagedDataSource.VirtualCount = VirtualItemCount; |
162 |
} |
163 |
else |
164 |
{ |
165 |
pagedDataSource = null ; |
166 |
return dataSource; |
167 |
} |
168 |
pagedDataSource.DataSource = dataSource; |
169 |
pagedDataSource.CurrentPageIndex = CurrentPageIndex; |
170 |
pagedDataSource.PageSize = PageSize; |
171 |
pagedDataSource.AllowPaging = true ; |
172 |
pagedDataSource.AllowCustomPaging = false ; |
173 |
ViewState[ "PageCount" ] = pagedDataSource.PageCount; |
174 |
return pagedDataSource; |
175 |
} |
176 |
return dataSource; |
177 |
} |
178 |
} |
179 |
} |
实现了带分页的Repeater控件。同理,也可以实现带分页的DataList。
4. GroupDropDownList
效果图:
5. SearchCloud标签云
效果图:
控件代码:
using System; |
002 |
using System.Collections.Generic; |
003 |
using System.Text; |
004 |
using System.Web; |
005 |
using System.Web.UI; |
006 |
using System.Web.UI.WebControls; |
007 |
using System.ComponentModel; |
008 |
using System.Data; |
009 |
using System.Collections; |
010 |
using System.Text.RegularExpressions; |
011 |
012 |
namespace Conovosoft.Web.UI.WebControls |
013 |
{ |
014 |
[ToolboxData( "<{0}:SearchCloud runat=server>" )] |
015 |
public class SearchCloud : WebControl |
016 |
{ |
017 |
#region "属性" |
018 |
019 |
#region "Appearance" |
020 |
021 |
[Bindable( true ), Category( "Appearance" ), Localizable( true )] |
022 |
public int MinFontSize |
023 |
{ |
024 |
get |
025 |
{ |
026 |
string s = ( string )ViewState[ "MinFontSize" ]; |
027 |
if (String.IsNullOrEmpty(s)) |
028 |
return 10; |
029 |
else |
030 |
{ |
031 |
int result = 10; |
032 |
int .TryParse(s, out result); |
033 |
return result; |
034 |
} |
035 |
} |
036 |
set |
037 |
{ |
038 |
ViewState[ "MinFontSize" ] = value; |
039 |
} |
040 |
} |
041 |
042 |
[Bindable( true ), Category( "Appearance" ), Localizable( true )] |
043 |
public int MaxFontSize |
044 |
{ |
045 |
get |
046 |
{ |
047 |
string s = ( string )ViewState[ "MaxFontSize" ]; |
048 |
if (String.IsNullOrEmpty(s)) |
049 |
return 22; |
050 |
else |
051 |
{ |
052 |
int result = 22; |
053 |
int .TryParse(s, out result); |
054 |
return result; |
055 |
} |
056 |
} |
057 |
set |
058 |
{ |
059 |
ViewState[ "MaxFontSize" ] = value; |
060 |
} |
061 |
} |
062 |
063 |
[Bindable( true ), Category( "Appearance" ), Localizable( true )] |
064 |
public string FontUint |
065 |
{ |
066 |
get |
067 |
{ |
068 |
string s = ( string )ViewState[ "FontUint" ]; |
069 |
if (String.IsNullOrEmpty(s)) |
070 |
return "pt" ; |
071 |
else |
072 |
return s; |
073 |
} |
074 |
set |
075 |
{ |
076 |
switch (value) |
077 |
{ |
078 |
case "pt" : |
079 |
case "em" : |
080 |
case "%" : |
081 |
case "px" : |
082 |
ViewState[ "FontUnit" ] = value; break ; |
083 |
default : |
084 |
ViewState[ "FontUnit" ] = "px" ; break ; |
085 |
} |
086 |
} |
087 |
} |
088 |
089 |
[Bindable( true ), Category( "Appearance" ), Localizable( true )] |
090 |
public string MaxColor |
091 |
{ |
092 |
get |
093 |
{ |
094 |
string s = ( string )ViewState[ "MaxColor" ]; |
095 |
096 |
if (String.IsNullOrEmpty(s)) |
097 |
return "#00f" ; |
098 |
else |
099 |
return s; |
100 |
} |
101 |
set |
102 |
{ |
103 |
ViewState[ "MaxColor" ] = value; |
104 |
} |
105 |
} |
106 |
107 |
[Bindable( true ), Category( "Appearance" ), Localizable( true )] |
108 |
public string MinColor |
109 |
{ |
110 |
get |
111 |
{ |
112 |
string s = ( string )ViewState[ "MinColor" ]; |
113 |
114 |
if (String.IsNullOrEmpty(s)) |
115 |
return "#000" ; |
116 |
else |
117 |
return s; |
118 |
} |
119 |
set |
120 |
{ |
121 |
ViewState[ "MinColor" ] = value; |
122 |
} |
123 |
} |
124 |
125 |
#endregion |
126 |
127 |
#region "Data" |
128 |
129 |
[Bindable( true ), Category( "Data" ), DefaultValue( "" )] |
130 |
public DataSet DataSource |
131 |
{ |
132 |
get |
133 |
{ |
134 |
return (DataSet)ViewState[ "DataSource" ]; |
135 |
} |
136 |
set |
137 |
{ |
138 |
ViewState[ "DataSource" ] = value; |
139 |
} |
140 |
} |
141 |
142 |
[Bindable( true ), Category( "Data" ), DefaultValue( "" ), Localizable( true )] |
143 |
public string DataIDField |
144 |
{ |
145 |
get |
146 |
{ |
147 |
return ( string )ViewState[ "DataIDField" ]; |
148 |
} |
149 |
set |
150 |
{ |
151 |
ViewState[ "DataIDField" ] = value; |
152 |
} |
153 |
} |
154 |
155 |
[Bindable( true ), Category( "Data" ), DefaultValue( "" ), Localizable( true )] |
156 |
public string DataKeywordField |
157 |
{ |
158 |
get |
159 |
{ |
160 |
return ( string )ViewState[ "DataKeywordField" ]; |
161 |
} |
162 |
set |
163 |
{ |
164 |
ViewState[ "DataKeywordField" ] = value; |
165 |
} |
166 |
} |
167 |
168 |
[Bindable( true ), Category( "Data" ), DefaultValue( "" ), Localizable( true )] |
169 |
public string DataURLField |
170 |
{ |
171 |
get |
172 |
{ |
173 |
return ( string )ViewState[ "DataURLField" ]; |
174 |
} |
175 |
set |
176 |
{ |
177 |
ViewState[ "DataURLField" ] = value; |
178 |
} |
179 |
} |
180 |
181 |
[Bindable( true ), Category( "Data" ), DefaultValue( "" ), Localizable( true )] |
182 |
public string DataCountField |
183 |
{ |
184 |
get |
185 |
{ |
186 |
return ( string )ViewState[ "DataCountField" ]; |
187 |
} |
188 |
set |
189 |
{ |
190 |
ViewState[ "DataCountField" ] = value; |
191 |
} |
192 |
} |
193 |
194 |
[Bindable( true ), Category( "Data" ), Localizable( true )] |
195 |
public string KeywordTitleFormat |
196 |
{ |
197 |
get |
198 |
{ |
199 |
string s = ( string )ViewState[ "KeywordTitleFormat" ]; |
200 |
if (String.IsNullOrEmpty(s)) |
201 |
{ |
202 |
return "%k occured %c times" ; |
203 |
} |
204 |
else |
205 |
{ |
206 |
return ( string )ViewState[ "KeywordTitleFormat" ]; |
207 |
} |
208 |
} |
209 |
set |
210 |
{ |
211 |
ViewState[ "KeywordTitleFormat" ] = value; |
212 |
} |
213 |
} |
214 |
215 |
[Bindable( true ), Category( "Data" ), Localizable( true )] |
216 |
public string KeywordURLFormat |
217 |
{ |
218 |
get |
219 |
{ |
220 |
return ( string )ViewState[ "KeywordURLFormat" ]; |
221 |
} |
222 |
set |
223 |
{ |
224 |
ViewState[ "KeywordURLFormat" ] = value; |
225 |
} |
226 |
} |
227 |
228 |
[Bindable( true ), Category( "Data" ), DefaultValue( "" ), Localizable( true )] |
229 |
public string SortBy |
230 |
{ |
231 |
get |
232 |
{ |
233 |
return ( string )ViewState[ "SortBy" ]; |
234 |
} |
235 |
set |
236 |
{ |
237 |
ViewState[ "SortBy" ] = value; |
238 |
} |
239 |
} |
240 |
241 |
#endregion |
242 |
243 |
[Bindable( false ), Category( "Debug" ), DefaultValue( false ), Localizable( true )] |
244 |
public Boolean Debug |
245 |
{ |
246 |
get |
247 |
{ |
248 |
if (ViewState[ "Debug" ] != null ) |
249 |
{ |
250 |
return (Boolean)ViewState[ "Debug" ]; |
251 |
} |
252 |
else |
253 |
{ |
254 |
return false ; |
255 |
} |
256 |
|
257 |
} |
258 |
set |
259 |
{ |
260 |
ViewState[ "Debug" ] = value; |
261 |
} |
262 |
} |
263 |
264 |
#region Private Properties |
265 |
266 |
private Hashtable arrAttributes; |
267 |
268 |
private string CloudHTML |
269 |
{ |
270 |
get |
271 |
{ |
272 |
string s = ( string )ViewState[ "CloudHTML" ]; |
273 |
if (String.IsNullOrEmpty(s)) |
274 |
{ |
275 |
return string .Empty; |
276 |
} |
277 |
return s; |
278 |
} |
279 |
set |
280 |
{ |
281 |
ViewState[ "CloudHTML" ] = value; |
282 |
} |
283 |
} |
284 |
285 |
private Hashtable KeyAttributes |
286 |
{ |
287 |
get |
288 |
{ |
289 |
return arrAttributes; |
290 |
} |
291 |
set |
292 |
{ |
293 |
arrAttributes = value; |
294 |
} |
295 |
} |
296 |
297 |
#endregion |
298 |
299 |
#endregion |
300 |
301 |
protected override void Render(HtmlTextWriter writer) |
302 |
{ |
303 |
if (!String.IsNullOrEmpty(CloudHTML)) |
304 |
{ |
305 |
writer.WriteBeginTag( "div" ); |
306 |
307 |
if (!String.IsNullOrEmpty(CssClass)) |
308 |
{ |
309 |
writer.WriteAttribute( "class" , CssClass); |
310 |
} |
311 |
writer.Write(HtmlTextWriter.TagRightChar); |
312 |
writer.Write(CloudHTML); |
313 |
writer.WriteEndTag( "div" ); |
314 |
} |
315 |
else |
316 |
{ |
317 |
writer.Write( "这儿没有产生HTML,一个未操作错误发生." ); |
318 |
} |
319 |
} |
320 |
321 |
protected override void OnLoad(EventArgs e) |
322 |
{ |
323 |
if (DataSource == null ) |
324 |
{ |
325 |
CloudHTML = "请指定DataSet" ; |
326 |
return ; |
327 |
} |
328 |
if (DataIDField == string .Empty) |
329 |
{ |
330 |
CloudHTML = "请指定一个ID数据段" ; |
331 |
return ; |
332 |
} |
333 |
if (DataKeywordField == string .Empty) |
334 |
{ |
335 |
CloudHTML = "请指定一个关键词数据段" ; |
336 |
return ; |
337 |
} |
338 |
if (DataCountField == string .Empty) |
339 |
{ |
340 |
CloudHTML = "请指定一个关键词数量数据段" ; |
341 |
return ; |
342 |
} |
343 |
if (!Regex.IsMatch(MinColor, "^#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$" )) |
344 |
{ |
345 |
CloudHTML = "最小颜色必须为十六进制编码并且必须为如: #000 or #ff99cc" ; |
346 |
return ; |
347 |
} |
348 |
if (!Regex.IsMatch(MaxColor, "^#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$" )) |
349 |
{ |
350 |
CloudHTML = "最小颜色必须为十六进制编码并且必须为如: #000 or #ff99cc" ; |
351 |
return ; |
352 |
} |
353 |
354 |
try |
355 |
{ |
356 |
StringBuilder sb = new StringBuilder(); |
357 |
DataView dv = new DataView(DataSource.Tables[0]); |
358 |
//DataRowView row; |
359 |
360 |
dv.Sort = string .Format( "{0} DESC" , DataCountField); |
361 |
362 |
int count = dv.Count; |
363 |
364 |
if (count == 0) |
365 |
{ |
366 |
CloudHTML = "没有任何值产生云" ; |
367 |
return ; |
368 |
} |
369 |
370 |
int MaxQty = int .Parse(dv[0].Row[DataCountField].ToString()); |
371 |
int MinQty = int .Parse(dv[dv.Count - 1].Row[DataCountField].ToString()); |
372 |
373 |
int Spread = MaxQty - MinQty; |
374 |
375 |
if (Spread == 0) |
376 |
Spread = 1; |
377 |
378 |
int FontSpread = MaxFontSize - MinFontSize; |
379 |
if (FontSpread == 0) |
380 |
FontSpread = 1; |
381 |
382 |
double FontStep = ( double )(FontSpread) / Spread; |
383 |
384 |
if (! string .IsNullOrEmpty(SortBy)) |
385 |
{ |
386 |
dv.Sort = SortBy; |
387 |
} |
388 |
else |
389 |
{ |
390 |
dv.Sort = string .Format( "{0} ASC" , DataKeywordField); |
391 |
} |
392 |
393 |
foreach (DataRowView row in dv) |
394 |
{ |
395 |
int sKeyID = int .Parse(row.Row[DataIDField].ToString()); |
396 |
string sKeyWord = row.Row[DataKeywordField].ToString(); |
397 |
int sKeyCount = int .Parse(row.Row[DataCountField].ToString()); |
398 |
string sKeyURL; |
399 |
string ColorRGB; |
400 |
double Weight = MinFontSize + ((sKeyCount - MinQty) * FontStep); |
401 |
int FontDiff = MaxFontSize - MinFontSize; |
402 |
double ColorWeight = Math.Round(99 * (Weight - MinFontSize) / (FontDiff) + 1); |
403 |
404 |
if (MinColor == MaxColor) |
405 |
{ |
406 |
ColorRGB = MinColor; |
407 |
} |
408 |
else |
409 |
{ |
410 |
ColorRGB = Colorize(MinColor, MaxColor, ColorWeight); |
411 |
} |
412 |
413 |
if (String.IsNullOrEmpty(DataURLField)) |
414 |
{ |
415 |
if (!String.IsNullOrEmpty(KeywordURLFormat)) |
416 |
{ |
417 |
sKeyURL = ReplaceKeyValues(KeywordURLFormat, sKeyID, sKeyWord, "" , sKeyCount); |
418 |
} |
419 |
else |
420 |
{ |
421 |
sKeyURL = "#" ; |
422 |
} |
423 |
} |
424 |
else |
425 |
{ |
426 |
sKeyURL = row[DataURLField].ToString(); |
427 |
} |
428 |
sb.Append( string .Format( "<a title=" \ " href=" \"{0}\" " {2}\?{6}?=" " font-size:{1}{5};color:{4};\??=" ">{3}</a> " , |
429 |
sKeyURL, |
430 |
Math.Round(Weight), |
431 |
ReplaceKeyValues(KeywordTitleFormat, sKeyID, sKeyWord, sKeyURL, sKeyCount), |
432 |
HttpContext.Current.Server.HtmlEncode(sKeyWord), |
433 |
ColorRGB, |
434 |
FontUint, |
435 |
GenerateAttributes(sKeyWord, sKeyID, sKeyURL, sKeyCount))); |
436 |
} |
437 |
CloudHTML = sb.ToString(); |
438 |
} |
439 |
catch (Exception ex) |
440 |
{ |
441 |
if (!Debug) |
442 |
{ |
443 |
CloudHTML = "错误产生" ; |
444 |
} |
445 |
else |
446 |
{ |
447 |
throw ex; |
448 |
} |
449 |
} |
450 |
finally |
451 |
{ |
452 |
base .OnLoad(e); |
453 |
} |
454 |
} |
455 |
456 |
public void AddAttribute( string value, string text) |
457 |
{ |
458 |
if (KeyAttributes == null ) |
459 |
{ |
460 |
KeyAttributes = new Hashtable(); |
461 |
} |
462 |
KeyAttributes.Add(value, text); |
463 |
} |
464 |
465 |
private string GenerateAttributes( string k, int id, string u, int c) |
466 |
{ |
467 |
if (KeyAttributes == null ) |
468 |
{ |
469 |
return string .Empty; |
470 |
} |
471 |
472 |
StringBuilder s = new StringBuilder(); |
473 |
ICollection keys = KeyAttributes.Keys; |
474 |
475 |
foreach ( object key in keys) |
476 |
{ |
477 |
s.Append( string .Format( " {0}=\"{1}\"" , key, ReplaceKeyValues(KeyAttributes[key].ToString(), id, k, u, c))); |
478 |
} |
479 |
480 |
return s.ToString(); |
481 |
} |
482 |
483 |
private string ReplaceKeyValues( string txt, int id, string k, string u, int c) |
484 |
{ |
485 |
k = k.Replace( "'" , "'" ); |
486 |
487 |
txt = txt.Replace( "%i" , id.ToString()); |
488 |
txt = txt.Replace( "%k" , HttpContext.Current.Server.HtmlEncode(k)); |
489 |
txt = txt.Replace( "%u" , u); |
490 |
txt = txt.Replace( "%c" , c.ToString()); |
491 |
492 |
return txt; |
493 |
} |
494 |
495 |
private string Colorize( string minc, string maxc, double w) |
496 |
{ |
497 |
w = w / 100; |
498 |
string rs, gs, bs; |
499 |
string r, g, b; |
500 |
int minr, ming, minb, maxr, maxg, maxb; |
501 |
502 |
if (minc.Length == 4) |
503 |
{ |
504 |
rs = minc.Substring(1, 1); |
505 |
gs = minc.Substring(2, 1); |
506 |
bs = minc.Substring(3, 1); |
507 |
508 |
minc = "#" + rs + rs + gs + gs + bs + bs; |
509 |
} |
510 |
511 |
if (maxc.Length == 4) |
512 |
{ |
513 |
rs = maxc.Substring(1, 1); |
514 |
gs = maxc.Substring(2, 1); |
515 |
bs = maxc.Substring(3, 1); |
516 |
517 |
maxc = "#" + rs + rs + gs + gs + bs + bs; |
518 |
} |
519 |
520 |
minr = Convert.ToInt32(minc.Substring(1, 2), 16); |
521 |
ming = Convert.ToInt32(minc.Substring(3, 2), 16); |
522 |
minb = Convert.ToInt32(minc.Substring(5, 2), 16); |
523 |
524 |
maxr = Convert.ToInt32(maxc.Substring(1, 2), 16); |
525 |
maxg = Convert.ToInt32(maxc.Substring(3, 2), 16); |
526 |
maxb = Convert.ToInt32(maxc.Substring(5, 2), 16); |
527 |
528 |
r = Convert.ToString( int .Parse(Math.Round(((maxr - minr) * w) + minr).ToString()), 16); |
529 |
g = Convert.ToString( int .Parse(Math.Round(((maxg - ming) * w) + ming).ToString()), 16); |
530 |
b = Convert.ToString( int .Parse(Math.Round(((maxb - minb) * w) + minb).ToString()), 16); |
531 |
532 |
if (r.Length == 1) |
533 |
r = "0" + r; |
534 |
if (g.Length == 1) |
535 |
g = "0" + g; |
536 |
if (b.Length == 1) |
537 |
b = "0" + b; |
538 |
539 |
string color; |
540 |
color = "#" + r + g + b; |
541 |
542 |
return color; |
543 |
} |
544 |
} |
545 |
} |
调用代码:
<cc1:SearchCloud ID="SearchCloud1" runat="server" />
后台绑定数据代码:
protected void Page_Load( object sender, EventArgs e) |
02 |
{ |
03 |
SqlConnection sqlcn = new SqlConnection( @"server=.\SQL2000;uid=sa;pwd=251099726;database=Northwind;" ); |
04 |
DataSet ds = new DataSet(); |
05 |
string sql = "select b.keyword_id,b.keyword_value,b.keyword_count from Products a, " ; |
06 |
sql += "(select top 10 [Products].ProductID as keyword_id, ProductName as keyword_value, " ; |
07 |
sql += "count([Order Details].ProductID) as keyword_count " ; |
08 |
sql += "from [Order Details],[Products] " ; |
09 |
sql += "where [Order Details].ProductID = [Products].ProductID " ; |
10 |
sql += "group by [Products].ProductID,ProductName order by keyword_count desc) as b " ; |
11 |
sql += "where a.ProductID = b.keyword_id " ; |
12 |
sql += "order by newid()" ; |
13 |
SqlDataAdapter da = new SqlDataAdapter(sql, sqlcn); |
14 |
da.Fill(ds); |
15 |
16 |
SearchCloud1.DataIDField = "keyword_id" ; |
17 |
SearchCloud1.DataKeywordField = "keyword_value" ; |
18 |
SearchCloud1.DataCountField = "keyword_count" ; |
19 |
SearchCloud1.DataSource = ds; |
20 |
} |
这样就实现了类似博客系统中的标签云。
最后附上我的源代码:WebControl.UI.rar,希望能够给大家带来些ASP.NET服务器控件开发上的帮助!