ListBox.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: ListBox
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2002)
  12. */
  13. using System;
  14. using System.Drawing;
  15. using System.Collections;
  16. using System.Collections.Specialized;
  17. using System.Globalization;
  18. using System.Web;
  19. using System.Web.UI;
  20. namespace System.Web.UI.WebControls
  21. {
  22. [ValidationProperty("SelectedItem")]
  23. public class ListBox : ListControl, IPostBackDataHandler
  24. {
  25. public ListBox(): base()
  26. {
  27. }
  28. public override Color BorderColor
  29. {
  30. get
  31. {
  32. return BorderColor;
  33. }
  34. set
  35. {
  36. BorderColor = value;
  37. }
  38. }
  39. public override BorderStyle BorderStyle
  40. {
  41. get
  42. {
  43. return BorderStyle;
  44. }
  45. set
  46. {
  47. BorderStyle = value;
  48. }
  49. }
  50. public override Unit BorderWidth
  51. {
  52. get
  53. {
  54. return BorderWidth;
  55. }
  56. set
  57. {
  58. BorderWidth = value;
  59. }
  60. }
  61. public virtual int Rows
  62. {
  63. get
  64. {
  65. object o = ViewState["Rows"];
  66. if(o != null)
  67. return (int)o;
  68. return 4;
  69. }
  70. set
  71. {
  72. if(value < 1 || value > 2000)
  73. {
  74. throw new ArgumentOutOfRangeException();
  75. }
  76. ViewState["Rows"] = value;
  77. }
  78. }
  79. public virtual ListSelectionMode SelectionMode
  80. {
  81. get
  82. {
  83. object o = ViewState["SelectionMode"];
  84. if(o != null)
  85. return (ListSelectionMode)o;
  86. return ListSelectionMode.Single;
  87. }
  88. set
  89. {
  90. if(!Enum.IsDefined(typeof(ListSelectionMode), value))
  91. {
  92. throw new ArgumentException();
  93. }
  94. ViewState["SelectionMode"] = value;
  95. }
  96. }
  97. public override string ToolTip
  98. {
  99. get
  100. {
  101. return String.Empty;
  102. }
  103. set
  104. {
  105. // Don't do anything.
  106. }
  107. }
  108. protected override void AddAttributesToRender(HtmlTextWriter writer)
  109. {
  110. if(Page != null)
  111. {
  112. Page.VerifyRenderingInServerForm(this);
  113. }
  114. writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
  115. writer.AddAttribute(HtmlTextWriterAttribute.Size, Rows.ToString(NumberFormatInfo.InvariantInfo));
  116. writer.AddAttribute(HtmlTextWriterAttribute.Multiple, "multiple");
  117. if(AutoPostBack && Page != null)
  118. {
  119. writer.AddAttribute(HtmlTextWriterAttribute.Onchange, Page.GetPostBackClientEvent(this, ""));
  120. writer.AddAttribute("language", "javascript");
  121. }
  122. }
  123. protected override void OnPreRender(EventArgs e)
  124. {
  125. base.OnPreRender(e);
  126. if(Page != null && SelectionMode == ListSelectionMode.Multiple && Enabled)
  127. {
  128. Page.RegisterRequiresPostBack(this);
  129. }
  130. }
  131. protected override void RenderContents(HtmlTextWriter writer)
  132. {
  133. bool isMultAllowed = (SelectionMode == ListSelectionMode.Multiple);
  134. bool selMade = false;
  135. foreach(ListItem current in Items)
  136. {
  137. writer.WriteBeginTag("option");
  138. if(current.Selected)
  139. {
  140. if(!isMultAllowed && selMade)
  141. {
  142. throw new HttpException("Cannnot_MutliSelect_In_Single_Mode");
  143. }
  144. selMade = true;
  145. writer.WriteAttribute("selected", "selected");
  146. }
  147. writer.WriteAttribute("value", current.Value, true);
  148. writer.Write('>');
  149. writer.Write(HttpUtility.HtmlEncode(current.Text));
  150. writer.WriteEndTag("option");
  151. writer.WriteLine();
  152. }
  153. }
  154. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
  155. {
  156. string[] vals = postCollection.GetValues(postDataKey);
  157. bool updated = false;
  158. ArrayList selected = SelectedIndices;
  159. ArrayList final = new ArrayList(vals.Length);
  160. if(vals != null)
  161. {
  162. if(SelectionMode == ListSelectionMode.Single)
  163. {
  164. int index = Items.FindByValueInternal(vals[0]);
  165. if(SelectedIndex != index)
  166. {
  167. SelectedIndex = index;
  168. updated = true;
  169. }
  170. } else
  171. {
  172. foreach(string current in vals)
  173. {
  174. final.Add(Items.FindByValueInternal(current));
  175. }
  176. if(selected != null && selected.Count == vals.Length)
  177. {
  178. for(int ctr = 0; ctr < vals.Length; ctr++)
  179. {
  180. if(((int)final[ctr]) != ((int)selected[ctr]))
  181. {
  182. updated = true;
  183. break;
  184. }
  185. }
  186. } else
  187. {
  188. updated = true;
  189. }
  190. }
  191. if(!updated)
  192. {
  193. Select(final);
  194. }
  195. } else
  196. {
  197. if(SelectedIndex != -1)
  198. SelectedIndex = -1;
  199. updated = true;
  200. }
  201. return updated;
  202. }
  203. void IPostBackDataHandler.RaisePostDataChangedEvent()
  204. {
  205. OnSelectedIndexChanged(EventArgs.Empty);
  206. }
  207. }
  208. }