1
using System;
2
using System.Web;
3
using System.Web.UI;
4
using System.Collections;
5
using System.Web.UI.WebControls;
6
7
8
namespace SunY.Web.UI.WebControls
9
{
10
[ToolboxData("<{0}:DropDownList runat=\"server\"></{0}:DropDownList>")]
11
public class DropDownList : System.Web.UI.WebControls.DropDownList
12
{
13
private ArrayList m_Items;
14
15
protected override object SaveViewState()
16
{
17
ArrayList alstViewState = new ArrayList();
18
object objBaseVS = base.SaveViewState();
19
alstViewState.Add(objBaseVS);
20
alstViewState.Add(m_Items);
21
22
return alstViewState;
23
}
24
25
26
protected override void LoadViewState(object savedState)
27
{
28
if ( savedState != null )
29
{
30
ArrayList alstViewState = (ArrayList)savedState;
31
if ( alstViewState.Count >= 1 )
32
{
33
base.LoadViewState(alstViewState[0]);
34
}
35
if ( alstViewState.Count == 2 )
36
{
37
m_Items = (ArrayList)alstViewState[1];
38
}
39
}
40
}
41
42
43
protected override void OnPreRender(EventArgs e)
44
{
45
base.OnPreRender (e);
46
47
if(m_Items == null)
48
{
49
m_Items = new ArrayList();
50
}
51
52
bool IsChange = false;
53
54
if(m_Items.Count == this.Items.Count)
55
{
56
for(int i = 0; i < this.Items.Count; i++)
57
{
58
sListItem sli = new sListItem(this.Items[i]);
59
if(!((sListItem)m_Items[i]).Equals(sli))
60
{
61
IsChange = true;
62
break;
63
}
64
}
65
}
66
else
67
{
68
IsChange = true;
69
}
70
71
if(IsChange)
72
{
73
m_Items.Clear();
74
75
ArrayList newList = new ArrayList();
76
77
foreach(ListItem li in this.Items)
78
{
79
sListItem sli = new sListItem(li);
80
81
m_Items.Add(sli);
82
}
83
}
84
85
86
}
87
88
89
protected override void RenderContents(HtmlTextWriter writer)
90
{
91
if(m_Items != null && this.Items != null)
92
{
93
if(this.Items.Count == m_Items.Count)
94
{
95
bool selected = false;
96
97
for(int i = 0; i < this.Items.Count; i++)
98
{
99
writer.WriteBeginTag("option");
100
101
if (this.Items[i].Selected)
102
{
103
if (selected)
104
{
105
throw new HttpException("Cannot multiselect in DropDownList.");
106
}
107
108
selected = true;
109
writer.WriteAttribute("selected", "true", false);
110
}
111
112
writer.WriteAttribute("value", this.Items[i].Value, true);
113
114
string styleValue = String.Empty;
115
116
foreach(ItemStyle s in ((sListItem)m_Items[i]).ItemStyles)
117
{
118
styleValue += String.Concat(s.Key, ":", s.KeyValue, ";");
119
}
120
121
writer.WriteAttribute("style", styleValue, false);
122
123
this.Items[i].Attributes.Render(writer);
124
writer.Write('>');
125
HttpUtility.HtmlEncode(this.Items[i].Text, writer);
126
writer.WriteEndTag("option");
127
writer.WriteLine();
128
}
129
}
130
}
131
}
132
}
133
134
[Serializable]
135
class ItemStyle
136
{
137
private string key;
138
private string keyValue;
139
140
public string Key
141
{
142
get
143
{
144
return key;
145
}
146
set
147
{
148
key = value;
149
}
150
}
151
152
public string KeyValue
153
{
154
get
155
{
156
return keyValue;
157
}
158
set
159
{
160
keyValue = value;
161
}
162
}
163
164
public ItemStyle(string key, string kvalue)
165
{
166
this.key = key;
167
this.keyValue = kvalue;
168
}
169
}
170
171
172
[Serializable]
173
class sListItem
174
{
175
private string _text;
176
private string _value;
177
private bool _selected;
178
private ArrayList _itemStyle;
179
180
public string Text
181
{
182
get
183
{
184
return _text;
185
}
186
set
187
{
188
_text = value;
189
}
190
}
191
192
193
public string Value
194
{
195
get
196
{
197
return _value;
198
}
199
set
200
{
201
_value = value;
202
}
203
}
204
205
206
public ArrayList ItemStyles
207
{
208
get
209
{
210
return _itemStyle;
211
}
212
set
213
{
214
_itemStyle = value;
215
}
216
}
217
218
219
public bool Selected
220
{
221
get
222
{
223
return _selected;
224
}
225
set
226
{
227
_selected = value;
228
}
229
}
230
231
232
public sListItem()
233
{
234
_text = String.Empty;
235
_value = String.Empty;
236
_itemStyle = new ArrayList();
237
_selected = false;
238
}
239
240
241
public sListItem(ListItem li)
242
{
243
this.Text = li.Text;
244
this.Value = li.Value;
245
this.Selected = li.Selected;
246
this._itemStyle = new ArrayList();
247
248
foreach(string key in li.Attributes.CssStyle.Keys)
249
{
250
ItemStyle style = new ItemStyle(key, li.Attributes.CssStyle[key]);
251
this._itemStyle.Add(style);
252
}
253
}
254
255
256
public virtual bool Equals(sListItem item)
257
{
258
if(this.Text == item.Text && this.Value == item.Value)
259
{
260
return true;
261
}
262
263
return false;
264
}
265
266
}
267
}
268
using System;2
using System.Web;3
using System.Web.UI;4
using System.Collections;5
using System.Web.UI.WebControls;6

7

8
namespace SunY.Web.UI.WebControls9
{10
[ToolboxData("<{0}:DropDownList runat=\"server\"></{0}:DropDownList>")] 11
public class DropDownList : System.Web.UI.WebControls.DropDownList12
{13
private ArrayList m_Items;14

15
protected override object SaveViewState() 16
{ 17
ArrayList alstViewState = new ArrayList(); 18
object objBaseVS = base.SaveViewState(); 19
alstViewState.Add(objBaseVS); 20
alstViewState.Add(m_Items); 21

22
return alstViewState; 23
} 24
25

26
protected override void LoadViewState(object savedState) 27
{ 28
if ( savedState != null ) 29
{ 30
ArrayList alstViewState = (ArrayList)savedState; 31
if ( alstViewState.Count >= 1 ) 32
{ 33
base.LoadViewState(alstViewState[0]); 34
} 35
if ( alstViewState.Count == 2 ) 36
{ 37
m_Items = (ArrayList)alstViewState[1]; 38
} 39
} 40
} 41

42

43
protected override void OnPreRender(EventArgs e)44
{45
base.OnPreRender (e);46

47
if(m_Items == null)48
{49
m_Items = new ArrayList();50
}51

52
bool IsChange = false;53

54
if(m_Items.Count == this.Items.Count)55
{56
for(int i = 0; i < this.Items.Count; i++)57
{58
sListItem sli = new sListItem(this.Items[i]);59
if(!((sListItem)m_Items[i]).Equals(sli))60
{61
IsChange = true;62
break;63
}64
}65
}66
else67
{68
IsChange = true;69
}70

71
if(IsChange)72
{73
m_Items.Clear();74

75
ArrayList newList = new ArrayList();76

77
foreach(ListItem li in this.Items)78
{79
sListItem sli = new sListItem(li);80

81
m_Items.Add(sli);82
} 83
}84
85

86
}87

88

89
protected override void RenderContents(HtmlTextWriter writer)90
{91
if(m_Items != null && this.Items != null)92
{93
if(this.Items.Count == m_Items.Count)94
{95
bool selected = false;96

97
for(int i = 0; i < this.Items.Count; i++)98
{99
writer.WriteBeginTag("option");100

101
if (this.Items[i].Selected)102
{103
if (selected)104
{105
throw new HttpException("Cannot multiselect in DropDownList.");106
}107

108
selected = true;109
writer.WriteAttribute("selected", "true", false);110
}111

112
writer.WriteAttribute("value", this.Items[i].Value, true);113

114
string styleValue = String.Empty;115

116
foreach(ItemStyle s in ((sListItem)m_Items[i]).ItemStyles)117
{118
styleValue += String.Concat(s.Key, ":", s.KeyValue, ";"); 119
}120

121
writer.WriteAttribute("style", styleValue, false);122

123
this.Items[i].Attributes.Render(writer); 124
writer.Write('>');125
HttpUtility.HtmlEncode(this.Items[i].Text, writer);126
writer.WriteEndTag("option");127
writer.WriteLine();128
}129
}130
}131
}132
}133

134
[Serializable]135
class ItemStyle136
{137
private string key;138
private string keyValue;139

140
public string Key141
{142
get143
{144
return key;145
}146
set147
{148
key = value;149
}150
}151

152
public string KeyValue153
{154
get155
{156
return keyValue;157
}158
set159
{160
keyValue = value;161
}162
}163

164
public ItemStyle(string key, string kvalue)165
{166
this.key = key;167
this.keyValue = kvalue;168
}169
}170

171
172
[Serializable]173
class sListItem174
{175
private string _text;176
private string _value;177
private bool _selected;178
private ArrayList _itemStyle;179

180
public string Text181
{182
get183
{184
return _text;185
}186
set187
{188
_text = value;189
}190
}191

192

193
public string Value194
{195
get196
{197
return _value;198
}199
set200
{201
_value = value;202
}203
}204

205

206
public ArrayList ItemStyles207
{208
get209
{210
return _itemStyle;211
}212
set213
{214
_itemStyle = value;215
}216
}217

218

219
public bool Selected220
{221
get222
{223
return _selected;224
}225
set226
{227
_selected = value;228
}229
}230

231

232
public sListItem()233
{234
_text = String.Empty;235
_value = String.Empty;236
_itemStyle = new ArrayList();237
_selected = false;238
}239
240

241
public sListItem(ListItem li)242
{243
this.Text = li.Text;244
this.Value = li.Value;245
this.Selected = li.Selected;246
this._itemStyle = new ArrayList();247
248
foreach(string key in li.Attributes.CssStyle.Keys)249
{250
ItemStyle style = new ItemStyle(key, li.Attributes.CssStyle[key]);251
this._itemStyle.Add(style);252
}253
}254

255

256
public virtual bool Equals(sListItem item)257
{258
if(this.Text == item.Text && this.Value == item.Value)259
{260
return true;261
}262

263
return false;264
}265

266
}267
}268




浙公网安备 33010602011771号