ListBox.cs 4.5 KB

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