ListBox.cs 4.4 KB

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