ListBox.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. base.AddAttributesToRender (writer);
  128. #if NET_2_0
  129. if (ID != null)
  130. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  131. #else
  132. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  133. #endif
  134. if (AutoPostBack)
  135. #if NET_2_0
  136. writer.AddAttribute (HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true));
  137. #else
  138. writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
  139. Page.ClientScript.GetPostBackClientHyperlink (this, ""));
  140. #endif
  141. if (SelectionMode == ListSelectionMode.Multiple)
  142. writer.AddAttribute (HtmlTextWriterAttribute.Multiple,
  143. "multiple");
  144. writer.AddAttribute (HtmlTextWriterAttribute.Size,
  145. Rows.ToString (CultureInfo.InvariantCulture));
  146. }
  147. #if NET_2_0
  148. PostBackOptions GetPostBackOptions () {
  149. PostBackOptions options = new PostBackOptions (this);
  150. options.ActionUrl = null;
  151. options.ValidationGroup = null;
  152. options.Argument = "";
  153. options.RequiresJavaScriptProtocol = false;
  154. options.ClientSubmit = true;
  155. options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
  156. if (options.PerformValidation)
  157. options.ValidationGroup = ValidationGroup;
  158. return options;
  159. }
  160. #endif
  161. #if !NET_2_0
  162. protected override void RenderContents (HtmlTextWriter writer)
  163. {
  164. foreach (ListItem item in Items) {
  165. writer.WriteBeginTag ("option");
  166. if (item.Selected) {
  167. writer.WriteAttribute ("selected", "selected", false);
  168. }
  169. writer.WriteAttribute ("value", item.Value, true);
  170. writer.Write (">");
  171. string encoded = HttpUtility.HtmlEncode (item.Text);
  172. writer.Write (encoded);
  173. writer.WriteEndTag ("option");
  174. writer.WriteLine ();
  175. }
  176. }
  177. #endif
  178. #if NET_2_0
  179. protected internal
  180. #else
  181. protected
  182. #endif
  183. override void OnPreRender (EventArgs e)
  184. {
  185. base.OnPreRender (e);
  186. if (Page != null && Enabled)
  187. Page.RegisterRequiresPostBack (this);
  188. }
  189. #if NET_2_0
  190. protected virtual
  191. #endif
  192. bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  193. {
  194. #if NET_2_0
  195. EnsureDataBound ();
  196. #endif
  197. string [] values = postCollection.GetValues (postDataKey);
  198. if (values == null || values.Length == 0) {
  199. int prev_index = SelectedIndex;
  200. SelectedIndex = -1;
  201. return (prev_index != -1);
  202. }
  203. if (SelectionMode == ListSelectionMode.Single)
  204. return SelectSingle (values);
  205. return SelectMultiple (values);
  206. }
  207. bool SelectSingle (string [] values)
  208. {
  209. string val = values [0];
  210. int idx = Items.IndexOf (val);
  211. int prev_index = SelectedIndex;
  212. if (idx != prev_index) {
  213. // This will set both the index value and the item.Selected property
  214. SelectedIndex = idx;
  215. return true;
  216. }
  217. return false;
  218. }
  219. bool SelectMultiple (string [] values)
  220. {
  221. ArrayList prev_selected = GetSelectedIndicesInternal ();
  222. ClearSelection ();
  223. foreach (string val in values) {
  224. ListItem item = Items.FindByValue (val);
  225. if (item != null)
  226. item.Selected = true;
  227. }
  228. ArrayList new_selection = GetSelectedIndicesInternal ();
  229. int i = prev_selected.Count;
  230. if (new_selection.Count != i)
  231. return true;
  232. while (--i >= 0) {
  233. if ((int) prev_selected [i] != (int) new_selection [i])
  234. return true;
  235. }
  236. return false;
  237. }
  238. #if NET_2_0
  239. protected virtual
  240. #endif
  241. void RaisePostDataChangedEvent ()
  242. {
  243. #if NET_2_0
  244. if (CausesValidation)
  245. Page.Validate (ValidationGroup);
  246. #endif
  247. OnSelectedIndexChanged (EventArgs.Empty);
  248. }
  249. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  250. NameValueCollection postCollection)
  251. {
  252. return LoadPostData (postDataKey, postCollection);
  253. }
  254. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  255. {
  256. RaisePostDataChangedEvent ();
  257. }
  258. #if NET_2_0
  259. protected internal override void VerifyMultiSelect()
  260. {
  261. //by default the ListControl will throw an exception in this method,
  262. //therefor we should override the method if the class is supporting
  263. //MultiSelect option.
  264. if (this.SelectionMode != ListSelectionMode.Multiple)
  265. throw new HttpException("Multiple select option is not supported");
  266. }
  267. #endif
  268. }
  269. }