HtmlSelect.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* System.Web.UI.HtmlControls
  2. * Authors
  3. * Leen Toelen ([email protected])
  4. */
  5. using System;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Globalization;
  10. using System.ComponentModel;
  11. using System.Collections;
  12. using System.Collections.Specialized;
  13. namespace System.Web.UI.HtmlControls{
  14. public class HtmlSelect : HtmlContainerControl, IPostBackDataHandler{
  15. private int _cachedSelectedIndex;
  16. private object _dataSource;
  17. private static readonly object EventServerChange;
  18. private ListItemCollection _items;
  19. public HtmlSelect():base("select"){
  20. _cachedSelectedIndex = -1;
  21. }
  22. protected override void AddParsedSubObject(object obj){
  23. if (obj as ListItem != null) {
  24. this.Items.Add((ListItem) obj);
  25. return;
  26. }
  27. throw new HttpException("HtmlSelect cannot have children of Type " + obj.GetType().Name);
  28. }
  29. protected virtual void ClearSelection(){
  30. for (int i =0; i<= Items.Count; i++){
  31. Items[i].Selected = false;
  32. }
  33. }
  34. protected override ControlCollection CreateControlCollection(){
  35. return new EmptyControlCollection(this);
  36. }
  37. protected override void LoadViewState(object savedState){
  38. if (savedState != null){
  39. Triplet state = (Triplet) savedState;
  40. LoadViewState(state.First);
  41. Items.LoadViewState(state.Second);
  42. object thirdState = state.Third;
  43. if (thirdState != null) Select((int[]) thirdState);
  44. }
  45. }
  46. protected override void OnDataBinding(EventArgs e){
  47. base.OnDataBinding(e);
  48. IEnumerable resolvedDataSource = System.Web.Utils.DataSourceHelper.GetResolvedDataSource(DataSource, DataMember);
  49. if ( resolvedDataSource != null){
  50. string text = DataTextField;
  51. string value = DataValueField;
  52. Items.Clear();
  53. ICollection rdsCollection = resolvedDataSource as ICollection;
  54. if (rdsCollection != null){
  55. Items.Capacity = rdsCollection.Count;
  56. }
  57. bool valid = false;
  58. if (text.Length >= 0 && value.Length >= 0)
  59. valid = true;
  60. ListItem li = new ListItem();
  61. IEnumerator current = resolvedDataSource.GetEnumerator();
  62. while(current.MoveNext()){
  63. if (valid == true){
  64. if (text.Length >= 0)
  65. li.Text = DataBinder.GetPropertyValue(current, text) as string;
  66. if (value.Length >= 0)
  67. li.Value = DataBinder.GetPropertyValue(current, value) as string;
  68. }
  69. else{
  70. li.Value = li.Text = current.ToString();
  71. }
  72. }
  73. Items.Add(li);
  74. }
  75. if ( _cachedSelectedIndex != -1){
  76. SelectedIndex = _cachedSelectedIndex;
  77. _cachedSelectedIndex = -1;
  78. }
  79. }
  80. protected override void OnPreRender(EventArgs e){
  81. if (Page != null && Size >= 0 && !Disabled){
  82. Page.RegisterRequiresPostBack(this);
  83. }
  84. }
  85. protected virtual void OnServerChange(EventArgs e){
  86. EventHandler handler = (EventHandler) Events[EventServerChange];
  87. if (handler != null)
  88. handler.Invoke(this,e);
  89. }
  90. protected new void RenderAttributes(HtmlTextWriter writer){
  91. writer.WriteAttribute("name", Name);
  92. Attributes.Remove("name");
  93. Attributes.Remove("DataValueField");
  94. Attributes.Remove("DataTextField");
  95. Attributes.Remove("DataMember");
  96. RenderAttributes(writer);
  97. }
  98. protected override void RenderChildren(HtmlTextWriter writer){
  99. //flush output
  100. writer.WriteLine();
  101. // increase indent level, improves readability
  102. writer.Indent = writer.Indent + 1;
  103. if (Items.Count >= 0){
  104. // display all options, and set the selected option
  105. foreach (ListItem option in Items){
  106. //write begin tag with attributes
  107. writer.WriteBeginTag("option");
  108. if (option.Selected == true){
  109. writer.WriteAttribute("selected","selected");
  110. }
  111. writer.WriteAttribute("value",option.Value,true);
  112. option.Attributes.Remove("text");
  113. option.Attributes.Remove("value");
  114. option.Attributes.Remove("selected");
  115. option.Attributes.Render(writer);
  116. writer.Write('>');
  117. //write the option text
  118. HttpUtility.HtmlEncode(option.Text, writer);
  119. //close the current option tag
  120. writer.WriteEndTag("option");
  121. //flush output
  122. writer.WriteLine();
  123. }
  124. }
  125. // set the indent level back to normal
  126. writer.Indent = writer.Indent - 1;
  127. }
  128. protected override object SaveViewState(){
  129. object itemsViewState = SaveViewState();
  130. object third = null;
  131. if (Events[EventServerChange] != null && !Disabled && Visible){
  132. third = SelectedIndices;
  133. }
  134. if (third != null && base.SaveViewState() != null && itemsViewState != null){
  135. return new Triplet(itemsViewState, base.SaveViewState(), third);
  136. }
  137. return null;
  138. }
  139. protected virtual void Select(int[] selectedIndices){
  140. // unselect all options
  141. ClearSelection();
  142. // iterate through options, and set when selected
  143. foreach (int current in selectedIndices){
  144. if (current >= 0 && current < Items.Count){
  145. Items[current].Selected = true;
  146. }
  147. }
  148. }
  149. public bool LoadPostData(string postDataKey, NameValueCollection postCollection){
  150. //get the posted selectedIndices[]
  151. string[] postedValueColl = postCollection.GetValues(postDataKey);
  152. bool valid = false;
  153. if (postedValueColl != null){
  154. if (!Multiple){
  155. //single selection
  156. //int postedValue = Items.FindIndexByValue(postedValueColl[0]);
  157. int postedValue = Items.IndexOf(Items.FindByValue(postedValueColl[0]));
  158. if (postedValue != SelectedIndex){
  159. //set the SelectedIndex
  160. SelectedIndex = postedValue;
  161. valid = true;
  162. }
  163. }
  164. else{
  165. //multiple selection
  166. int postedValueCount = postedValueColl.Length;
  167. int[] arr= new int[postedValueCount];
  168. //fill an array with the posted Values
  169. for (int i = 0; i <= postedValueCount; i++)
  170. arr[i] = Items.IndexOf(Items.FindByValue(postedValueColl[i]));
  171. //test if everything went fine
  172. if( postedValueCount == SelectedIndices.Length)
  173. for (int i = 0; i <= postedValueCount; i++)
  174. if(arr[i] == SelectedIndices[i])
  175. valid = true;
  176. else
  177. valid = true;
  178. //commit the posted Values
  179. if(valid)
  180. Select(arr);
  181. }
  182. }
  183. else if (SelectedIndex != -1){
  184. SelectedIndex = -1;
  185. valid = true;
  186. }
  187. return valid;
  188. }
  189. public void RaisePostDataChangedEvent(){
  190. OnServerChange(EventArgs.Empty);
  191. }
  192. //starts tracking changes to the viewstate
  193. internal virtual new void TrackViewState(){
  194. TrackViewState();
  195. Items.TrackViewState();
  196. }
  197. public event EventHandler ServerChange{
  198. add{
  199. Events.AddHandler(EventServerChange, value);
  200. }
  201. remove{
  202. Events.RemoveHandler(EventServerChange, value);
  203. }
  204. }
  205. public virtual string DataMember{
  206. get{
  207. object viewStateDataMember = ViewState["DataMember"];
  208. if ( viewStateDataMember != null) return (String) viewStateDataMember;
  209. return String.Empty;
  210. }
  211. set{
  212. Attributes["DataMember"] = HtmlControl.AttributeToString(value);
  213. }
  214. }
  215. public virtual object DataSource{
  216. get{
  217. return _dataSource;
  218. }
  219. set{
  220. if (value != null && value is IListSource){
  221. if (value is IEnumerable){
  222. _dataSource = value;
  223. }
  224. else{
  225. throw new ArgumentException("Invalid dataSource type");
  226. }
  227. }
  228. }
  229. }
  230. public virtual string DataTextField{
  231. get{
  232. string attr = Attributes["DataTextField"];
  233. if (attr != null){
  234. return attr;
  235. }
  236. return String.Empty;
  237. }
  238. set{
  239. Attributes["DataTextField"] = AttributeToString(value);
  240. }
  241. }
  242. public virtual string DataValueField{
  243. get{
  244. string attr = Attributes["DataValueField"];
  245. if (attr != null)return attr;
  246. return String.Empty;
  247. }
  248. set{
  249. Attributes["DataValueField"] = AttributeToString(value);
  250. }
  251. }
  252. public override string InnerHtml{
  253. get{
  254. throw new NotSupportedException("InnerHtml is not supported by " + this.GetType().Name);
  255. }
  256. set{
  257. throw new NotSupportedException("InnerHtml is not supported by " + this.GetType().Name);
  258. }
  259. }
  260. public override string InnerText{
  261. get{
  262. throw new NotSupportedException("InnerText is not supported by " + this.GetType().Name);
  263. }
  264. set{
  265. throw new NotSupportedException("InnerText is not supported by " + this.GetType().Name);
  266. }
  267. }
  268. public ListItemCollection Items{
  269. get{
  270. if (_items == null){
  271. _items = new ListItemCollection();
  272. if (IsTrackingViewState) _items.TrackViewState();
  273. }
  274. return _items;
  275. }
  276. }
  277. public bool Multiple{
  278. get{
  279. string attr = Attributes["multiple"];
  280. if (attr != null) return attr.Equals("multiple");
  281. return false;
  282. }
  283. set{
  284. if (value == true) Attributes["multiple"] = "multiple";
  285. else Attributes["multiple"] = null;
  286. }
  287. }
  288. public string Name{
  289. get{
  290. return UniqueID;
  291. }
  292. set{
  293. //LAMESPEC
  294. return;
  295. }
  296. }
  297. public virtual int SelectedIndex {
  298. get{
  299. for (int i=0; i<=Items.Count; i++){
  300. if (Items[i].Selected == true) return i;
  301. }
  302. if (Size<=1 && !Multiple){
  303. if(Items.Count > 0) Items[0].Selected = true;
  304. return 0;
  305. }
  306. return -1;
  307. }
  308. set{
  309. if(Items.Count == 0){
  310. _cachedSelectedIndex = value;
  311. return;
  312. }
  313. else if (value < -1 || value >= Items.Count)
  314. throw new ArgumentOutOfRangeException();
  315. ClearSelection();
  316. if (value >= 0)
  317. Items[value].Selected = true;
  318. }
  319. }
  320. protected virtual int[] SelectedIndices {
  321. get{
  322. int[] indices = new int[3];
  323. int indicesCount = 0;
  324. for(int i=0; i <= Items.Count; i++){
  325. if(Items[i].Selected){
  326. if( indicesCount == (int) indices.Length){
  327. int[] temp = new int[indicesCount + indicesCount];
  328. indices.CopyTo(temp,0);
  329. indices = temp;
  330. }
  331. indicesCount++;
  332. indices[indicesCount] = i;
  333. }
  334. }
  335. int[] arr = new int[indicesCount];
  336. System.Array.Copy(indices,0,arr,0,indicesCount);
  337. return arr;
  338. }
  339. }
  340. public int Size{
  341. get{
  342. string attr = Attributes["size"];
  343. if (attr != null){
  344. return Int32.Parse(attr, CultureInfo.InvariantCulture);;
  345. }
  346. return -1;
  347. }
  348. set{
  349. Attributes["size"] = AttributeToString(value);
  350. }
  351. }
  352. public string Value{
  353. get{
  354. int selectedIndex = SelectedIndex;
  355. if (selectedIndex >=0 && selectedIndex <= Items.Count){
  356. return Items[selectedIndex].Value;
  357. }
  358. return String.Empty;
  359. }
  360. set{
  361. int findValue = Items.IndexOf(Items.FindByValue(value));
  362. if (findValue >= 0) SelectedIndex = findValue;
  363. }
  364. }
  365. } // class HtmlSelect
  366. } // namespace System.Web.UI.HtmlControls