ListControl.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. //
  2. // System.Web.UI.WebControls.ListControl.cs
  3. //
  4. // Authors:
  5. // Jackson Harper ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Drawing;
  30. using System.Web.Util;
  31. using System.Collections;
  32. using System.Globalization;
  33. using System.ComponentModel;
  34. using System.Collections.Specialized;
  35. namespace System.Web.UI.WebControls {
  36. [DataBindingHandler("System.Web.UI.Design.WebControls.ListControlDataBindingHandler, " + Consts.AssemblySystem_Design)]
  37. [DefaultEventAttribute ("SelectedIndexChanged")]
  38. [DefaultPropertyAttribute ("DataSource")]
  39. [Designer("System.Web.UI.Design.WebControls.ListControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  40. #if NET_2_0
  41. [ControlValueProperty ("SelectedValue", null)]
  42. [ParseChildrenAttribute (true, "Items", ChildControlType = typeof (Control))]
  43. #else
  44. [ParseChildrenAttribute (true, "Items")]
  45. #endif
  46. public abstract class ListControl :
  47. #if NET_2_0
  48. DataBoundControl, IEditableTextControl, ITextControl
  49. #else
  50. WebControl
  51. #endif
  52. {
  53. private static readonly object SelectedIndexChangedEvent = new object ();
  54. #if NET_2_0
  55. private static readonly object TextChangedEvent = new object ();
  56. #endif
  57. private object data_source;
  58. private ListItemCollection items;
  59. public ListControl () : base (HtmlTextWriterTag.Select)
  60. {
  61. }
  62. #if NET_2_0
  63. [DefaultValue (false)]
  64. [Themeable (false)]
  65. [MonoTODO]
  66. [WebSysDescription ("")]
  67. [WebCategory ("Behavior")]
  68. public virtual bool AppendDataBoundItems
  69. {
  70. get {
  71. throw new NotImplementedException ();
  72. }
  73. set {
  74. throw new NotImplementedException ();
  75. }
  76. }
  77. #endif
  78. #if NET_2_0
  79. [Themeable (false)]
  80. #endif
  81. [DefaultValue(false)]
  82. [WebSysDescription ("")]
  83. [WebCategory ("Behavior")]
  84. public virtual bool AutoPostBack {
  85. get { return ViewState.GetBool ("AutoPostBack", false); }
  86. set { ViewState ["AutoPostBack"] = value; }
  87. }
  88. #if ONLY_1_1
  89. [DefaultValue("")]
  90. [WebSysDescription ("")]
  91. [WebCategory ("Data")]
  92. public virtual string DataMember {
  93. get { return ViewState.GetString ("DataMember", String.Empty); }
  94. set { ViewState ["DataMember"] = value; }
  95. }
  96. [Bindable(true)]
  97. [DefaultValue(null)]
  98. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  99. [WebSysDescription ("")]
  100. [WebCategory ("Data")]
  101. public virtual object DataSource {
  102. get { return data_source; }
  103. set { data_source = value; }
  104. }
  105. #endif
  106. #if NET_2_0
  107. [Themeable (false)]
  108. #endif
  109. [DefaultValue("")]
  110. [WebSysDescription ("")]
  111. [WebCategory ("Data")]
  112. public virtual string DataTextField {
  113. get { return ViewState.GetString ("DataTextField", String.Empty); }
  114. set { ViewState ["DataTextField"] = value; }
  115. }
  116. #if NET_2_0
  117. [Themeable (false)]
  118. #endif
  119. [DefaultValue("")]
  120. [WebSysDescription ("")]
  121. [WebCategory ("Data")]
  122. public virtual string DataTextFormatString {
  123. get { return ViewState.GetString ("DataTextFormatString", String.Empty); }
  124. set { ViewState ["DataTextFormatString"] = value; }
  125. }
  126. #if NET_2_0
  127. [Themeable (false)]
  128. #endif
  129. [DefaultValue("")]
  130. [WebSysDescription ("")]
  131. [WebCategory ("Data")]
  132. public virtual string DataValueField {
  133. get { return ViewState.GetString ("DataValueField", String.Empty); }
  134. set { ViewState ["DataValueField"] = value; }
  135. }
  136. #if NET_2_0
  137. [Editor ("System.Web.UI.Design.WebControls.ListItemsCollectionEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  138. #endif
  139. [DefaultValue(null)]
  140. [MergableProperty(false)]
  141. [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
  142. [WebSysDescription ("")]
  143. [WebCategory ("Misc")]
  144. public virtual ListItemCollection Items {
  145. get {
  146. if (items == null)
  147. items = new ListItemCollection ();
  148. return items;
  149. }
  150. }
  151. // I can't find this info stored in the viewstate anywhere
  152. // so it must be calculated on the fly.
  153. [Bindable(true)]
  154. [Browsable(false)]
  155. [DefaultValue(0)]
  156. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  157. #if NET_2_0
  158. [Themeable (false)]
  159. #endif
  160. [WebSysDescription ("")]
  161. [WebCategory ("Misc")]
  162. public virtual int SelectedIndex {
  163. get {
  164. if (items == null)
  165. return -1;
  166. for (int i = 0; i < items.Count; i++) {
  167. if (items [i].Selected)
  168. return i;
  169. }
  170. return -1;
  171. }
  172. set {
  173. ClearSelection ();
  174. if (value == -1 || items == null)
  175. return;
  176. if (value < 0 || value >= Items.Count)
  177. throw new ArgumentOutOfRangeException ("value");
  178. items [value].Selected = true;
  179. }
  180. }
  181. [Browsable(false)]
  182. [DefaultValue(null)]
  183. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  184. [WebSysDescription ("")]
  185. [WebCategory ("Misc")]
  186. public virtual ListItem SelectedItem {
  187. get {
  188. int si = SelectedIndex;
  189. if (si == -1)
  190. return null;
  191. return Items [si];
  192. }
  193. }
  194. #if NET_2_0
  195. [Bindable(true, BindingDirection.TwoWay)]
  196. [Themeable (false)]
  197. #else
  198. [Bindable(true)]
  199. #endif
  200. [Browsable(false)]
  201. [DefaultValue("")]
  202. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  203. [WebSysDescription ("")]
  204. [WebCategory ("Misc")]
  205. public virtual string SelectedValue {
  206. get {
  207. int si = SelectedIndex;
  208. if (si == -1)
  209. return String.Empty;
  210. return Items [si].Value;
  211. }
  212. set {
  213. ClearSelection ();
  214. for (int i = 0; i < Items.Count; i++) {
  215. if (Items [i].Value == value)
  216. Items [i].Selected = true;
  217. }
  218. }
  219. }
  220. #if NET_2_0
  221. [Themeable (false)]
  222. [DefaultValue ("")]
  223. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  224. [Browsable (false)]
  225. [MonoTODO]
  226. [WebSysDescription ("")]
  227. [WebCategoryAttribute ("Behavior")]
  228. public virtual string Text
  229. {
  230. get {
  231. throw new NotImplementedException ();
  232. }
  233. set {
  234. throw new NotImplementedException ();
  235. }
  236. }
  237. [MonoTODO]
  238. protected virtual new HtmlTextWriterTag TagKey
  239. {
  240. get {
  241. throw new NotImplementedException ();
  242. }
  243. }
  244. protected override void AddAttributesToRender (HtmlTextWriter w)
  245. {
  246. base.AddAttributesToRender (w);
  247. }
  248. #endif
  249. public virtual void ClearSelection ()
  250. {
  251. if (items == null)
  252. return;
  253. int count = Items.Count;
  254. for (int i = 0; i<count; i++)
  255. items [i].Selected = false;
  256. }
  257. #if NET_2_0
  258. [MonoTODO]
  259. protected internal override void LoadControlState (object savedState)
  260. {
  261. throw new NotImplementedException ();
  262. }
  263. #endif
  264. protected override void OnDataBinding (EventArgs e)
  265. {
  266. base.OnDataBinding (e);
  267. IEnumerable list = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
  268. if (list == null)
  269. return;
  270. Items.Clear ();
  271. foreach (object container in list) {
  272. string text;
  273. string value;
  274. if (DataTextField != String.Empty) {
  275. text = DataBinder.Eval (container,
  276. DataTextField).ToString ();
  277. } else {
  278. text = String.Empty;
  279. }
  280. if (DataValueField != String.Empty) {
  281. value = DataBinder.Eval (container,
  282. DataValueField).ToString ();
  283. } else {
  284. value = text;
  285. }
  286. if (text == String.Empty) {
  287. if (value != String.Empty)
  288. text = value;
  289. } else if (DataTextFormatString != String.Empty) {
  290. // Dont apply the format string if we don't actually
  291. // have a textfield
  292. text = String.Format (DataTextFormatString, text);
  293. }
  294. ListItem item = new ListItem (text, value);
  295. Items.Add (item);
  296. }
  297. }
  298. #if NET_2_0
  299. [MonoTODO]
  300. protected internal override void OnInit (EventArgs e)
  301. {
  302. throw new NotImplementedException ();
  303. }
  304. #endif
  305. #if NET_2_0
  306. protected internal
  307. #else
  308. protected
  309. #endif
  310. override void OnPreRender (EventArgs e)
  311. {
  312. base.OnPreRender (e);
  313. }
  314. #if NET_2_0
  315. [MonoTODO]
  316. protected virtual void OnTextChanged (EventArgs e)
  317. {
  318. throw new NotImplementedException ();
  319. }
  320. [MonoTODO]
  321. protected internal override void PerformDataBinding (IEnumerable dataSource)
  322. {
  323. throw new NotImplementedException ();
  324. }
  325. [MonoTODO]
  326. protected override void PerformSelect ()
  327. {
  328. throw new NotImplementedException ();
  329. }
  330. [MonoTODO]
  331. protected internal override void RenderContents (HtmlTextWriter w)
  332. {
  333. throw new NotImplementedException ();
  334. }
  335. [MonoTODO]
  336. protected internal override object SaveControlState ()
  337. {
  338. throw new NotImplementedException ();
  339. }
  340. #endif
  341. protected override object SaveViewState ()
  342. {
  343. object first = null;
  344. object second = null;
  345. ArrayList selected = null;
  346. first = base.SaveViewState ();
  347. IStateManager manager = items as IStateManager;
  348. if (manager != null)
  349. second = manager.SaveViewState ();
  350. if (items != null) {
  351. selected = new ArrayList ();
  352. int count = Items.Count;
  353. for (int i = 0; i < count; i++) {
  354. if (items [i].Selected)
  355. selected.Add (i);
  356. }
  357. }
  358. if (first == null && second == null && selected == null)
  359. return null;
  360. return new Triplet (first, second, selected);
  361. }
  362. protected override void LoadViewState (object savedState)
  363. {
  364. object first = null;
  365. object second = null;
  366. ArrayList indices = null;
  367. Triplet triplet = savedState as Triplet;
  368. if (triplet != null) {
  369. first = triplet.First;
  370. second = triplet.Second;
  371. indices = triplet.Third as ArrayList;
  372. }
  373. base.LoadViewState (first);
  374. if (second != null) {
  375. IStateManager manager = Items as IStateManager;
  376. manager.LoadViewState (second);
  377. }
  378. if (indices != null) {
  379. foreach (int index in indices)
  380. Items [index].Selected = true;
  381. }
  382. }
  383. #if NET_2_0
  384. [MonoTODO]
  385. protected void SetPostDataSelection (int selectedIndex)
  386. {
  387. throw new NotImplementedException ();
  388. }
  389. #endif
  390. protected override void TrackViewState ()
  391. {
  392. base.TrackViewState ();
  393. IStateManager manager = items as IStateManager;
  394. if (manager != null)
  395. manager.TrackViewState ();
  396. }
  397. protected virtual void OnSelectedIndexChanged (EventArgs e)
  398. {
  399. EventHandler handler = (EventHandler) Events [SelectedIndexChangedEvent];
  400. if (handler != null)
  401. handler (this, e);
  402. }
  403. #if NET_2_0
  404. protected internal virtual void VerifyMultiSelect ()
  405. {
  406. }
  407. #endif
  408. [WebSysDescription ("")]
  409. [WebCategory ("Action")]
  410. public event EventHandler SelectedIndexChanged {
  411. add { Events.AddHandler (SelectedIndexChangedEvent, value); }
  412. remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
  413. }
  414. #if NET_2_0
  415. /* sealed in the docs */
  416. public event EventHandler TextChanged {
  417. add {
  418. Events.AddHandler (TextChangedEvent, value);
  419. }
  420. remove {
  421. Events.RemoveHandler (TextChangedEvent, value);
  422. }
  423. }
  424. [MonoTODO]
  425. [Themeable (false)]
  426. [DefaultValue (false)]
  427. [WebSysDescription ("")]
  428. [WebCategory ("Behavior")]
  429. public virtual bool CausesValidation {
  430. get {
  431. throw new NotImplementedException ();
  432. }
  433. set {
  434. throw new NotImplementedException ();
  435. }
  436. }
  437. [MonoTODO]
  438. [Themeable (false)]
  439. [DefaultValue ("")]
  440. [WebSysDescription ("")]
  441. [WebCategoryAttribute ("Behavior")]
  442. public virtual string ValidationGroup {
  443. get {
  444. throw new NotImplementedException ();
  445. }
  446. set {
  447. throw new NotImplementedException ();
  448. }
  449. }
  450. #endif
  451. }
  452. }