ListBox.cs 4.5 KB

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