ListBox.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: ListBox
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2002)
  12. */
  13. using System;
  14. using System.Drawing;
  15. using System.Collections;
  16. using System.Collections.Specialized;
  17. using System.ComponentModel;
  18. using System.Globalization;
  19. using System.Web;
  20. using System.Web.UI;
  21. namespace System.Web.UI.WebControls
  22. {
  23. [DefaultEvent("SelectedIndexChanged")]
  24. [DefaultProperty("DataSource")]
  25. [ParseChildren(true)]
  26. [PersistChildren(false)]
  27. [ValidationProperty("SelectedItem")]
  28. public class ListBox : ListControl, IPostBackDataHandler
  29. {
  30. public ListBox () : base ()
  31. {
  32. }
  33. public override Color BorderColor
  34. {
  35. get { return base.BorderColor; }
  36. set { base.BorderColor = value; }
  37. }
  38. public override BorderStyle BorderStyle
  39. {
  40. get { return base.BorderStyle; }
  41. set { base.BorderStyle = value; }
  42. }
  43. public override Unit BorderWidth
  44. {
  45. get { return base.BorderWidth; }
  46. set { base.BorderWidth = value; }
  47. }
  48. public virtual int Rows
  49. {
  50. get {
  51. object o = ViewState ["Rows"];
  52. return (o == null) ? 4 : (int) o;
  53. }
  54. set {
  55. if (value < 1 || value > 2000)
  56. throw new ArgumentOutOfRangeException ();
  57. ViewState ["Rows"] = value;
  58. }
  59. }
  60. public virtual ListSelectionMode SelectionMode
  61. {
  62. get
  63. {
  64. object o = ViewState ["SelectionMode"];
  65. return (o == null) ? ListSelectionMode.Single : (ListSelectionMode) o;
  66. }
  67. set
  68. {
  69. if (!Enum.IsDefined (typeof (ListSelectionMode), value))
  70. throw new ArgumentException ();
  71. ViewState ["SelectionMode"] = value;
  72. }
  73. }
  74. public override string ToolTip
  75. {
  76. get { return String.Empty; }
  77. set { /* Don't do anything. */ }
  78. }
  79. protected override void AddAttributesToRender (HtmlTextWriter writer)
  80. {
  81. if (Page != null)
  82. Page.VerifyRenderingInServerForm (this);
  83. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  84. base.AddAttributesToRender (writer);
  85. writer.AddAttribute (HtmlTextWriterAttribute.Size,
  86. Rows.ToString (NumberFormatInfo.InvariantInfo));
  87. if (SelectionMode == ListSelectionMode.Multiple)
  88. writer.AddAttribute (HtmlTextWriterAttribute.Multiple, "multiple");
  89. if (AutoPostBack && Page != null){
  90. writer.AddAttribute (HtmlTextWriterAttribute.Onchange,
  91. Page.GetPostBackClientEvent (this, ""));
  92. writer.AddAttribute ("language", "javascript");
  93. }
  94. }
  95. protected override void OnPreRender (EventArgs e)
  96. {
  97. base.OnPreRender (e);
  98. if (Page != null && SelectionMode == ListSelectionMode.Multiple && Enabled)
  99. Page.RegisterRequiresPostBack(this);
  100. }
  101. protected override void RenderContents (HtmlTextWriter writer)
  102. {
  103. bool isMultAllowed = (SelectionMode == ListSelectionMode.Multiple);
  104. bool selMade = false;
  105. foreach (ListItem current in Items){
  106. writer.WriteBeginTag ("option");
  107. if (current.Selected){
  108. if (!isMultAllowed && selMade)
  109. throw new HttpException ("Cannot_MultiSelect_In_Single_Mode");
  110. selMade = true;
  111. writer.WriteAttribute ("selected", "selected");
  112. }
  113. writer.WriteAttribute ("value", current.Value, true);
  114. writer.Write ('>');
  115. writer.Write (HttpUtility.HtmlEncode (current.Text));
  116. writer.WriteEndTag ("option");
  117. writer.WriteLine ();
  118. }
  119. }
  120. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  121. NameValueCollection postCollection)
  122. {
  123. string[] vals = postCollection.GetValues (postDataKey);
  124. bool updated = false;
  125. ArrayList selected = SelectedIndices;
  126. ArrayList final = new ArrayList (vals.Length);
  127. if (vals != null){
  128. if (SelectionMode == ListSelectionMode.Single){
  129. int index = Items.FindByValueInternal (vals [0]);
  130. if (SelectedIndex != index){
  131. SelectedIndex = index;
  132. updated = true;
  133. }
  134. } else {
  135. foreach (string current in vals)
  136. final.Add (Items.FindByValueInternal (current));
  137. if (selected != null && selected.Count == vals.Length){
  138. for (int ctr = 0; ctr < vals.Length; ctr++){
  139. if (((int) final [ctr]) != ((int) selected [ctr])){
  140. updated = true;
  141. break;
  142. }
  143. }
  144. } else {
  145. updated = true;
  146. }
  147. }
  148. if (!updated)
  149. Select (final);
  150. } else {
  151. if (SelectedIndex != -1)
  152. SelectedIndex = -1;
  153. updated = true;
  154. }
  155. return updated;
  156. }
  157. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  158. {
  159. OnSelectedIndexChanged (EventArgs.Empty);
  160. }
  161. }
  162. }