ListBox.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. using System.Web.Util;
  35. namespace System.Web.UI.WebControls {
  36. // CAS
  37. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  38. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. // attributes
  40. [ValidationProperty("SelectedItem")]
  41. [SupportsEventValidation]
  42. public class ListBox : ListControl, IPostBackDataHandler {
  43. public ListBox ()
  44. {
  45. }
  46. [Browsable(false)]
  47. #if HAVE_CONTROL_ADAPTERS
  48. public virtual new
  49. #else
  50. public override
  51. #endif
  52. Color BorderColor {
  53. get { return base.BorderColor; }
  54. set { base.BorderColor = value; }
  55. }
  56. [Browsable(false)]
  57. #if HAVE_CONTROL_ADAPTERS
  58. public virtual new
  59. #else
  60. public override
  61. #endif
  62. BorderStyle BorderStyle {
  63. get { return base.BorderStyle; }
  64. set { base.BorderStyle = value; }
  65. }
  66. [Browsable(false)]
  67. #if HAVE_CONTROL_ADAPTERS
  68. public virtual new
  69. #else
  70. public override
  71. #endif
  72. Unit BorderWidth {
  73. get { return base.BorderWidth; }
  74. set { base.BorderWidth = value; }
  75. }
  76. [DefaultValue(4)]
  77. [WebSysDescription ("")]
  78. [WebCategory ("Appearance")]
  79. public virtual int Rows {
  80. get {
  81. return ViewState.GetInt ("Rows", 4);
  82. }
  83. set {
  84. if (value < 1 || value > 2000)
  85. throw new ArgumentOutOfRangeException ("value");
  86. ViewState ["Rows"] = value;
  87. }
  88. }
  89. [DefaultValue(ListSelectionMode.Single)]
  90. [WebSysDescription ("")]
  91. [WebCategory ("Behavior")]
  92. public virtual ListSelectionMode SelectionMode {
  93. get {
  94. return (ListSelectionMode) ViewState.GetInt ("SelectionMode",
  95. (int) ListSelectionMode.Single);
  96. }
  97. set {
  98. if (!Enum.IsDefined (typeof (ListSelectionMode), value))
  99. throw new ArgumentOutOfRangeException ("value");
  100. ViewState ["SelectionMode"] = value;
  101. }
  102. }
  103. public virtual int[] GetSelectedIndices ()
  104. {
  105. return (int []) GetSelectedIndicesInternal ().ToArray (typeof (int));
  106. }
  107. protected override void AddAttributesToRender (HtmlTextWriter writer)
  108. {
  109. if (Page != null)
  110. Page.VerifyRenderingInServerForm (this);
  111. if (ID != null)
  112. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  113. if (AutoPostBack) {
  114. string onchange = Page.ClientScript.GetPostBackEventReference (GetPostBackOptions (), true);
  115. onchange = String.Concat ("setTimeout('", onchange.Replace ("\\", "\\\\").Replace ("'", "\\'"), "', 0)");
  116. writer.AddAttribute (HtmlTextWriterAttribute.Onchange, BuildScriptAttribute ("onchange", onchange));
  117. }
  118. if (SelectionMode == ListSelectionMode.Multiple)
  119. writer.AddAttribute (HtmlTextWriterAttribute.Multiple,
  120. "multiple", false);
  121. writer.AddAttribute (HtmlTextWriterAttribute.Size,
  122. Rows.ToString (Helpers.InvariantCulture));
  123. base.AddAttributesToRender (writer);
  124. }
  125. PostBackOptions GetPostBackOptions () {
  126. PostBackOptions options = new PostBackOptions (this);
  127. options.ActionUrl = null;
  128. options.ValidationGroup = null;
  129. options.Argument = String.Empty;
  130. options.RequiresJavaScriptProtocol = false;
  131. options.ClientSubmit = true;
  132. options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
  133. if (options.PerformValidation)
  134. options.ValidationGroup = ValidationGroup;
  135. return options;
  136. }
  137. protected internal
  138. override void OnPreRender (EventArgs e)
  139. {
  140. base.OnPreRender (e);
  141. Page page = Page;
  142. if (page != null && IsEnabled)
  143. page.RegisterRequiresPostBack (this);
  144. }
  145. protected virtual
  146. bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  147. {
  148. EnsureDataBound ();
  149. string [] values = postCollection.GetValues (postDataKey);
  150. if (values == null || values.Length == 0) {
  151. int prev_index = SelectedIndex;
  152. SelectedIndex = -1;
  153. return (prev_index != -1);
  154. }
  155. ValidateEvent (UniqueID, values [0]);
  156. if (SelectionMode == ListSelectionMode.Single)
  157. return SelectSingle (values);
  158. return SelectMultiple (values);
  159. }
  160. bool SelectSingle (string [] values)
  161. {
  162. string val = values [0];
  163. int idx = Items.IndexOf (val);
  164. int prev_index = SelectedIndex;
  165. if (idx != prev_index) {
  166. // This will set both the index value and the item.Selected property
  167. SelectedIndex = idx;
  168. return true;
  169. }
  170. return false;
  171. }
  172. bool SelectMultiple (string [] values)
  173. {
  174. ArrayList prev_selected = GetSelectedIndicesInternal ();
  175. ClearSelection ();
  176. foreach (string val in values) {
  177. ListItem item = Items.FindByValue (val);
  178. if (item != null)
  179. item.Selected = true;
  180. }
  181. ArrayList new_selection = GetSelectedIndicesInternal ();
  182. int i = prev_selected.Count;
  183. if (new_selection.Count != i)
  184. return true;
  185. while (--i >= 0) {
  186. if ((int) prev_selected [i] != (int) new_selection [i])
  187. return true;
  188. }
  189. return false;
  190. }
  191. protected virtual
  192. void RaisePostDataChangedEvent ()
  193. {
  194. if (CausesValidation)
  195. Page.Validate (ValidationGroup);
  196. OnSelectedIndexChanged (EventArgs.Empty);
  197. }
  198. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  199. NameValueCollection postCollection)
  200. {
  201. return LoadPostData (postDataKey, postCollection);
  202. }
  203. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  204. {
  205. RaisePostDataChangedEvent ();
  206. }
  207. internal override bool MultiSelectOk ()
  208. {
  209. return this.SelectionMode == ListSelectionMode.Multiple;
  210. }
  211. }
  212. }