ListBox.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // System.Web.UI.WebControls.ListBox.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Drawing;
  33. using System.Collections;
  34. using System.Collections.Specialized;
  35. using System.ComponentModel;
  36. using System.Globalization;
  37. using System.Web;
  38. using System.Web.UI;
  39. namespace System.Web.UI.WebControls
  40. {
  41. [ValidationProperty("SelectedItem")]
  42. public class ListBox : ListControl, IPostBackDataHandler
  43. {
  44. public ListBox () : base ()
  45. {
  46. }
  47. [Browsable (false)]
  48. public override Color BorderColor
  49. {
  50. get { return base.BorderColor; }
  51. set { base.BorderColor = value; }
  52. }
  53. [Browsable (false)]
  54. public override BorderStyle BorderStyle
  55. {
  56. get { return base.BorderStyle; }
  57. set { base.BorderStyle = value; }
  58. }
  59. [Browsable (false)]
  60. public override Unit BorderWidth
  61. {
  62. get { return base.BorderWidth; }
  63. set { base.BorderWidth = value; }
  64. }
  65. [DefaultValue (4), Bindable (true), WebCategory ("Appearance")]
  66. [WebSysDescription ("The number of rows displayed by the control.")]
  67. public virtual int Rows
  68. {
  69. get {
  70. object o = ViewState ["Rows"];
  71. return (o == null) ? 4 : (int) o;
  72. }
  73. set {
  74. if (value < 1 || value > 2000)
  75. throw new ArgumentOutOfRangeException ("value", "Rows value has to be >= 0 and <= 2000.");
  76. ViewState ["Rows"] = value;
  77. }
  78. }
  79. [DefaultValue (typeof (ListSelectionMode), "Single"), WebCategory ("Behavior")]
  80. [WebSysDescription ("The mode describing how the entries can be selected.")]
  81. public virtual ListSelectionMode SelectionMode
  82. {
  83. get
  84. {
  85. object o = ViewState ["SelectionMode"];
  86. return (o == null) ? ListSelectionMode.Single : (ListSelectionMode) o;
  87. }
  88. set
  89. {
  90. if (!Enum.IsDefined (typeof (ListSelectionMode), value))
  91. throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
  92. ViewState ["SelectionMode"] = value;
  93. }
  94. }
  95. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  96. [Bindable (false), EditorBrowsable (EditorBrowsableState.Never)]
  97. public override string ToolTip
  98. {
  99. get { return String.Empty; }
  100. set { /* Don't do anything. */ }
  101. }
  102. protected override void AddAttributesToRender (HtmlTextWriter writer)
  103. {
  104. if (Page != null)
  105. Page.VerifyRenderingInServerForm (this);
  106. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  107. base.AddAttributesToRender (writer);
  108. writer.AddAttribute (HtmlTextWriterAttribute.Size,
  109. Rows.ToString (NumberFormatInfo.InvariantInfo));
  110. if (SelectionMode == ListSelectionMode.Multiple)
  111. writer.AddAttribute (HtmlTextWriterAttribute.Multiple, "multiple");
  112. if (AutoPostBack && Page != null){
  113. writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
  114. Page.GetPostBackClientEvent (this, ""));
  115. writer.AddAttribute ("language", "javascript");
  116. }
  117. }
  118. protected override void OnPreRender (EventArgs e)
  119. {
  120. base.OnPreRender (e);
  121. if (Page != null && SelectionMode == ListSelectionMode.Multiple && Enabled)
  122. Page.RegisterRequiresPostBack(this);
  123. }
  124. protected override void RenderContents (HtmlTextWriter writer)
  125. {
  126. bool isMultAllowed = (SelectionMode == ListSelectionMode.Multiple);
  127. bool selMade = false;
  128. foreach (ListItem current in Items){
  129. writer.WriteBeginTag ("option");
  130. if (current.Selected){
  131. if (!isMultAllowed && selMade)
  132. throw new HttpException ("Cannot_MultiSelect_In_Single_Mode");
  133. selMade = true;
  134. writer.WriteAttribute ("selected", "selected");
  135. }
  136. writer.WriteAttribute ("value", current.Value, true);
  137. writer.Write ('>');
  138. writer.Write (HttpUtility.HtmlEncode (current.Text));
  139. writer.WriteEndTag ("option");
  140. writer.WriteLine ();
  141. }
  142. }
  143. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  144. NameValueCollection postCollection)
  145. {
  146. string[] vals = postCollection.GetValues (postDataKey);
  147. bool updated = false;
  148. if (vals != null){
  149. if (SelectionMode == ListSelectionMode.Single){
  150. int index = Items.FindByValueInternal (vals [0]);
  151. if (SelectedIndex != index){
  152. SelectedIndex = index;
  153. updated = true;
  154. }
  155. } else {
  156. ArrayList indices = SelectedIndices;
  157. int length = vals.Length;
  158. ArrayList final = new ArrayList (length);
  159. foreach (string current in vals)
  160. final.Add (Items.FindByValueInternal (current));
  161. if (indices.Count == length) {
  162. for (int ctr = 0; ctr < length; ctr++){
  163. if (((int) final [ctr]) != ((int) indices [ctr])){
  164. updated = true;
  165. break;
  166. }
  167. }
  168. } else {
  169. updated = true;
  170. }
  171. if (updated)
  172. Select (final);
  173. }
  174. } else {
  175. if (SelectedIndex != -1)
  176. SelectedIndex = -1;
  177. updated = true;
  178. }
  179. return updated;
  180. }
  181. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  182. {
  183. OnSelectedIndexChanged (EventArgs.Empty);
  184. }
  185. }
  186. }