ListBox.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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;
  29. using System.ComponentModel;
  30. using System.Drawing;
  31. using System.Globalization;
  32. using System.Collections.Specialized;
  33. namespace System.Web.UI.WebControls {
  34. [ValidationProperty("SelectedItem")]
  35. public class ListBox : ListControl, IPostBackDataHandler {
  36. public ListBox ()
  37. {
  38. }
  39. [Browsable(false)]
  40. #if NET_2_0 && HAVE_CONTROL_ADAPTERS
  41. public virtual new
  42. #else
  43. public override
  44. #endif
  45. Color BorderColor {
  46. get { return base.BorderColor; }
  47. set { base.BorderColor = value; }
  48. }
  49. [Browsable(false)]
  50. #if NET_2_0 && HAVE_CONTROL_ADAPTERS
  51. public virtual new
  52. #else
  53. public override
  54. #endif
  55. BorderStyle BorderStyle {
  56. get { return base.BorderStyle; }
  57. set { base.BorderStyle = value; }
  58. }
  59. [Browsable(false)]
  60. #if NET_2_0 && HAVE_CONTROL_ADAPTERS
  61. public virtual new
  62. #else
  63. public override
  64. #endif
  65. Unit BorderWidth {
  66. get { return base.BorderWidth; }
  67. set { base.BorderWidth = value; }
  68. }
  69. #if ONLY_1_1
  70. [Bindable(true)]
  71. #endif
  72. [DefaultValue(4)]
  73. [WebSysDescription ("")]
  74. [WebCategory ("Appearance")]
  75. #if NET_2_0
  76. public virtual
  77. #else
  78. public
  79. #endif
  80. int Rows {
  81. get {
  82. return ViewState.GetInt ("Rows", 4);
  83. }
  84. set {
  85. if (value < 1 || value > 2000)
  86. throw new ArgumentOutOfRangeException ("value");
  87. ViewState ["Rows"] = value;
  88. }
  89. }
  90. [DefaultValue(ListSelectionMode.Single)]
  91. [WebSysDescription ("")]
  92. [WebCategory ("Behavior")]
  93. #if NET_2_0
  94. public virtual
  95. #else
  96. public
  97. #endif
  98. ListSelectionMode SelectionMode {
  99. get {
  100. return (ListSelectionMode) ViewState.GetInt ("SelectionMode",
  101. (int) ListSelectionMode.Single);
  102. }
  103. set {
  104. if (!Enum.IsDefined (typeof (ListSelectionMode), value))
  105. throw new ArgumentOutOfRangeException ("value");
  106. ViewState ["SelectionMode"] = value;
  107. }
  108. }
  109. #if ONLY_1_1
  110. [Bindable(false)]
  111. [Browsable(false)]
  112. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  113. [EditorBrowsable(EditorBrowsableState.Never)]
  114. public override string ToolTip {
  115. get { return String.Empty; }
  116. set { /* Tooltip is always String.Empty */ }
  117. }
  118. #endif
  119. #if NET_2_0
  120. [MonoTODO]
  121. public virtual int[] GetSelectedIndices ()
  122. {
  123. throw new NotImplementedException ();
  124. }
  125. #endif
  126. protected override void AddAttributesToRender (HtmlTextWriter writer)
  127. {
  128. if (Page != null)
  129. Page.VerifyRenderingInServerForm (this);
  130. writer.AddAttribute (HtmlTextWriterAttribute.Name, ClientID);
  131. if (SelectionMode == ListSelectionMode.Multiple)
  132. writer.AddAttribute (HtmlTextWriterAttribute.Multiple,
  133. "multiple");
  134. writer.AddAttribute (HtmlTextWriterAttribute.Size,
  135. Rows.ToString (CultureInfo.InvariantCulture));
  136. }
  137. #if NET_2_0
  138. protected internal
  139. #else
  140. protected
  141. #endif
  142. override void RenderContents (HtmlTextWriter writer)
  143. {
  144. base.RenderContents (writer);
  145. foreach (ListItem item in Items) {
  146. writer.WriteBeginTag ("option");
  147. if (item.Selected) {
  148. writer.WriteAttribute ("selected", "selected", false);
  149. }
  150. writer.WriteAttribute ("value", item.Value, true);
  151. writer.Write (">");
  152. writer.Write (item.Text);
  153. writer.WriteEndTag ("option");
  154. writer.WriteLine ();
  155. }
  156. }
  157. #if NET_2_0
  158. protected internal
  159. #else
  160. protected
  161. #endif
  162. override void OnPreRender (EventArgs e)
  163. {
  164. base.OnPreRender (e);
  165. if (Page != null)
  166. Page.RegisterRequiresPostBack (this);
  167. }
  168. #if NET_2_0
  169. [MonoTODO]
  170. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  171. {
  172. throw new NotImplementedException ();
  173. }
  174. [MonoTODO]
  175. protected virtual void RaisePostDataChangedEvent ()
  176. {
  177. throw new NotImplementedException ();
  178. }
  179. #endif
  180. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  181. NameValueCollection postCollection)
  182. {
  183. string [] items = postCollection.GetValues (postDataKey) as string [];
  184. bool res = false;
  185. if (items == null)
  186. return false;
  187. foreach (string value in items) {
  188. ListItem item = Items.FindByValue (value);
  189. if (!item.Selected) {
  190. item.Selected = true;
  191. res = true;
  192. }
  193. }
  194. // So we can tell when they have been changed
  195. Items.TrackViewState ();
  196. return res;
  197. }
  198. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  199. {
  200. OnSelectedIndexChanged (EventArgs.Empty);
  201. }
  202. }
  203. }