ListBox.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // System.Web.UI.WebControls.Literal.cs
  3. //
  4. // Authors:
  5. // Jackson Harper ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Collections;
  29. using System.ComponentModel;
  30. using System.Drawing;
  31. using System.Globalization;
  32. using System.Collections.Specialized;
  33. using System.Security.Permissions;
  34. namespace System.Web.UI.WebControls {
  35. // CAS
  36. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. // attributes
  39. [ValidationProperty("SelectedItem")]
  40. #if NET_2_0
  41. [SupportsEventValidation]
  42. #endif
  43. public class ListBox : ListControl, IPostBackDataHandler {
  44. public ListBox ()
  45. {
  46. }
  47. [Browsable(false)]
  48. #if NET_2_0 && HAVE_CONTROL_ADAPTERS
  49. public virtual new
  50. #else
  51. public override
  52. #endif
  53. Color BorderColor {
  54. get { return base.BorderColor; }
  55. set { base.BorderColor = value; }
  56. }
  57. [Browsable(false)]
  58. #if NET_2_0 && HAVE_CONTROL_ADAPTERS
  59. public virtual new
  60. #else
  61. public override
  62. #endif
  63. BorderStyle BorderStyle {
  64. get { return base.BorderStyle; }
  65. set { base.BorderStyle = value; }
  66. }
  67. [Browsable(false)]
  68. #if NET_2_0 && HAVE_CONTROL_ADAPTERS
  69. public virtual new
  70. #else
  71. public override
  72. #endif
  73. Unit BorderWidth {
  74. get { return base.BorderWidth; }
  75. set { base.BorderWidth = value; }
  76. }
  77. #if ONLY_1_1
  78. [Bindable(true)]
  79. #endif
  80. [DefaultValue(4)]
  81. [WebSysDescription ("")]
  82. [WebCategory ("Appearance")]
  83. public virtual int Rows {
  84. get {
  85. return ViewState.GetInt ("Rows", 4);
  86. }
  87. set {
  88. if (value < 1 || value > 2000)
  89. throw new ArgumentOutOfRangeException ("value");
  90. ViewState ["Rows"] = value;
  91. }
  92. }
  93. [DefaultValue(ListSelectionMode.Single)]
  94. [WebSysDescription ("")]
  95. [WebCategory ("Behavior")]
  96. public virtual ListSelectionMode SelectionMode {
  97. get {
  98. return (ListSelectionMode) ViewState.GetInt ("SelectionMode",
  99. (int) ListSelectionMode.Single);
  100. }
  101. set {
  102. if (!Enum.IsDefined (typeof (ListSelectionMode), value))
  103. throw new ArgumentOutOfRangeException ("value");
  104. ViewState ["SelectionMode"] = value;
  105. }
  106. }
  107. #if ONLY_1_1
  108. [Bindable(false)]
  109. [Browsable(false)]
  110. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  111. [EditorBrowsable(EditorBrowsableState.Never)]
  112. public override string ToolTip {
  113. get { return String.Empty; }
  114. set { /* Tooltip is always String.Empty */ }
  115. }
  116. #endif
  117. #if NET_2_0
  118. public virtual int[] GetSelectedIndices ()
  119. {
  120. return (int []) GetSelectedIndicesInternal ().ToArray (typeof (int));
  121. }
  122. #endif
  123. protected override void AddAttributesToRender (HtmlTextWriter writer)
  124. {
  125. if (Page != null)
  126. Page.VerifyRenderingInServerForm (this);
  127. #if NET_2_0
  128. if (ID != null)
  129. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  130. #else
  131. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  132. #endif
  133. if (AutoPostBack) {
  134. #if NET_2_0
  135. string onchange = Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
  136. onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
  137. writer.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
  138. #else
  139. writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
  140. BuildScriptAttribute ("onchange",
  141. Page.ClientScript.GetPostBackClientHyperlink (this, "")));
  142. #endif
  143. }
  144. if (SelectionMode == ListSelectionMode.Multiple)
  145. writer.AddAttribute (HtmlTextWriterAttribute.Multiple,
  146. "multiple", false);
  147. writer.AddAttribute (HtmlTextWriterAttribute.Size,
  148. Rows.ToString (CultureInfo.InvariantCulture));
  149. base.AddAttributesToRender (writer);
  150. }
  151. #if NET_2_0
  152. PostBackOptions GetPostBackOptions () {
  153. PostBackOptions options = new PostBackOptions (this);
  154. options.ActionUrl = null;
  155. options.ValidationGroup = null;
  156. options.Argument = "";
  157. options.RequiresJavaScriptProtocol = false;
  158. options.ClientSubmit = true;
  159. options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
  160. if (options.PerformValidation)
  161. options.ValidationGroup = ValidationGroup;
  162. return options;
  163. }
  164. #endif
  165. #if ONLY_1_1
  166. protected override void RenderContents (HtmlTextWriter writer)
  167. {
  168. foreach (ListItem item in Items) {
  169. if (item.Selected) {
  170. writer.AddAttribute (HtmlTextWriterAttribute.Selected, "selected", false);
  171. }
  172. writer.AddAttribute (HtmlTextWriterAttribute.Value, item.Value, true);
  173. writer.RenderBeginTag (HtmlTextWriterTag.Option);
  174. string encoded = HttpUtility.HtmlEncode (item.Text);
  175. writer.Write (encoded);
  176. writer.RenderEndTag ();
  177. writer.WriteLine ();
  178. }
  179. }
  180. #endif
  181. #if NET_2_0
  182. protected internal
  183. #else
  184. protected
  185. #endif
  186. override void OnPreRender (EventArgs e)
  187. {
  188. base.OnPreRender (e);
  189. if (Page != null && Enabled)
  190. Page.RegisterRequiresPostBack (this);
  191. }
  192. #if NET_2_0
  193. protected virtual
  194. #endif
  195. bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  196. {
  197. #if NET_2_0
  198. EnsureDataBound ();
  199. #endif
  200. string [] values = postCollection.GetValues (postDataKey);
  201. if (values == null || values.Length == 0) {
  202. int prev_index = SelectedIndex;
  203. SelectedIndex = -1;
  204. return (prev_index != -1);
  205. }
  206. if (SelectionMode == ListSelectionMode.Single)
  207. return SelectSingle (values);
  208. return SelectMultiple (values);
  209. }
  210. bool SelectSingle (string [] values)
  211. {
  212. string val = values [0];
  213. int idx = Items.IndexOf (val);
  214. int prev_index = SelectedIndex;
  215. if (idx != prev_index) {
  216. // This will set both the index value and the item.Selected property
  217. SelectedIndex = idx;
  218. return true;
  219. }
  220. return false;
  221. }
  222. bool SelectMultiple (string [] values)
  223. {
  224. ArrayList prev_selected = GetSelectedIndicesInternal ();
  225. ClearSelection ();
  226. foreach (string val in values) {
  227. ListItem item = Items.FindByValue (val);
  228. if (item != null)
  229. item.Selected = true;
  230. }
  231. ArrayList new_selection = GetSelectedIndicesInternal ();
  232. int i = prev_selected.Count;
  233. if (new_selection.Count != i)
  234. return true;
  235. while (--i >= 0) {
  236. if ((int) prev_selected [i] != (int) new_selection [i])
  237. return true;
  238. }
  239. return false;
  240. }
  241. #if NET_2_0
  242. protected virtual
  243. #endif
  244. void RaisePostDataChangedEvent ()
  245. {
  246. #if NET_2_0
  247. if (CausesValidation)
  248. Page.Validate (ValidationGroup);
  249. #endif
  250. OnSelectedIndexChanged (EventArgs.Empty);
  251. }
  252. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  253. NameValueCollection postCollection)
  254. {
  255. return LoadPostData (postDataKey, postCollection);
  256. }
  257. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  258. {
  259. RaisePostDataChangedEvent ();
  260. }
  261. #if NET_2_0
  262. protected internal override void VerifyMultiSelect()
  263. {
  264. //by default the ListControl will throw an exception in this method,
  265. //therefor we should override the method if the class is supporting
  266. //MultiSelect option.
  267. if (this.SelectionMode != ListSelectionMode.Multiple)
  268. throw new HttpException("Multiple select option is not supported");
  269. }
  270. #endif
  271. }
  272. }