ListBox.cs 4.5 KB

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