-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtendedDropDownList.ascx.cs
197 lines (168 loc) · 5.64 KB
/
ExtendedDropDownList.ascx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace Wokhan.WebControls
{
[ToolboxData("<{0}:ExtendedDropDownList runat=server></{0}:ExtendedDropDownList>")]
[SupportsEventValidation, ValidationProperty("SelectedItem")]
public class ExtendedDropDownList : DropDownList
{
public class ListItemMixedCollection : List<object>
{
private ListItemCollection owner;
internal ListItemMixedCollection(ListItemCollection src)
{
owner = src;
}
public new void Add(object item)
{
if (item is ListItem)
{
base.Add(item);
owner.Add((ListItem)item);
}
else
{
throw new ArgumentException("The Add method only accept ListItem objects.");
}
}
public ListItemGroup AddGroup(string groupName)
{
var group = new ListItemGroup(owner) { Label = groupName };
base.Add(group);
return group;
}
public new void Insert(int index, object item)
{
if (item is ListItem)
{
base.Insert(index, item);
owner.Insert(index, (ListItem)item);
}
else
{
throw new ArgumentException("The Add method only accept ListItem objects.");
}
}
public ListItemGroup InsertGroup(int index, string groupName)
{
var group = new ListItemGroup(owner) { Label = groupName };
base.Add(group);
return group;
}
}
public class WrappedListItemCollection : List<ListItem>
{
ListItemCollection _lst;
internal WrappedListItemCollection(ListItemCollection lst)
{
_lst = lst;
}
public void Add(ListItem item)
{
base.Add(item);
_lst.Add(item);
}
public void Insert(int index, ListItem item)
{
base.Insert(index, item);
_lst.Insert(index, item);
}
}
public class ListItemGroup
{
public string Label { get; set; }
public bool Enabled { get; set; }
private System.Web.UI.AttributeCollection _attributes = new System.Web.UI.AttributeCollection(new StateBag(true));
public System.Web.UI.AttributeCollection Attributes { get { return _attributes; } }
private WrappedListItemCollection _items;
public WrappedListItemCollection Items { get { return _items; } }
public ListItemGroup(ListItemCollection src)
{
_items = new WrappedListItemCollection(src);
}
}
private ListItemMixedCollection _items;
public new ListItemMixedCollection Items { get { return _items; } }
public ExtendedDropDownList()
{
_items = new ListItemMixedCollection(base.Items);
}
protected override void RenderContents(HtmlTextWriter writer)
{
foreach (var g in Items)
{
if (g is ListItem)
{
RenderItem((ListItem)g, writer);
}
else
{
RenderItemGroup((ListItemGroup)g, writer);
}
}
}
private void RenderItemGroup(ListItemGroup g, HtmlTextWriter writer)
{
if (g.Label != null)
{
writer.WriteBeginTag("optgroup");
writer.WriteAttribute("label", g.Label);
writer.Write(HtmlTextWriter.TagRightChar);
}
foreach (ListItem li in g.Items)
{
RenderItem(li, writer);
}
if (g.Label != null)
{
writer.WriteEndTag("optgroup");
}
}
private void RenderItem(ListItem li, HtmlTextWriter writer)
{
bool selected = false;
writer.WriteBeginTag("option");
if (li.Selected || base.Items.FindByValue(li.Value).Selected)
{
if (selected)
{
VerifyMultiSelect();
}
selected = true;
writer.WriteAttribute("selected", "selected");
}
if (!li.Enabled)
{
writer.WriteAttribute("disabled", "disabled");
}
writer.WriteAttribute("value", li.Value, true);
// VSWhidbey 163920 Render expando attributes.
if (li.Attributes != null && li.Attributes.Count > 0)
{
li.Attributes.Render(writer);
}
if (Page != null)
{
Page.ClientScript.RegisterForEventValidation(UniqueID, li.Value);
}
writer.Write(HtmlTextWriter.TagRightChar);
HttpUtility.HtmlEncode(li.Text, writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
}
}