ListControl.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. #if !NET_2_0
  39. [DefaultPropertyAttribute ("DataSource")]
  40. #endif
  41. [Designer("System.Web.UI.Design.WebControls.ListControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  42. [ParseChildrenAttribute (true, "Items")]
  43. #if NET_2_0
  44. [ControlValueProperty ("SelectedValue", null)]
  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 ListItemCollection items;
  58. int saved_selected_index = -2;
  59. string saved_selected_value;
  60. public ListControl () : base (HtmlTextWriterTag.Select)
  61. {
  62. }
  63. #if NET_2_0
  64. [DefaultValue (false)]
  65. [Themeable (false)]
  66. [MonoTODO]
  67. [WebSysDescription ("")]
  68. [WebCategory ("Behavior")]
  69. public virtual bool AppendDataBoundItems
  70. {
  71. get {
  72. return ViewState.GetBool ("AppendDataBoundItems", false);
  73. }
  74. set {
  75. ViewState ["AppendDataBoundItems"] = value;
  76. }
  77. }
  78. #endif
  79. #if NET_2_0
  80. [Themeable (false)]
  81. #endif
  82. [DefaultValue(false)]
  83. [WebSysDescription ("")]
  84. [WebCategory ("Behavior")]
  85. public virtual bool AutoPostBack {
  86. get { return ViewState.GetBool ("AutoPostBack", false); }
  87. set { ViewState ["AutoPostBack"] = value; }
  88. }
  89. #if ONLY_1_1
  90. [DefaultValue("")]
  91. [WebSysDescription ("")]
  92. [WebCategory ("Data")]
  93. public virtual string DataMember {
  94. get { return ViewState.GetString ("DataMember", String.Empty); }
  95. set { ViewState ["DataMember"] = value; }
  96. }
  97. private object data_source;
  98. [Bindable(true)]
  99. [DefaultValue(null)]
  100. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  101. [WebSysDescription ("")]
  102. [WebCategory ("Data")]
  103. public virtual object DataSource {
  104. get { return data_source; }
  105. set {
  106. if(value == null || value is IListSource || value is IEnumerable) {
  107. data_source = value;
  108. return;
  109. }
  110. throw new ArgumentException("Invalid DataSource Type");
  111. }
  112. }
  113. #endif
  114. #if NET_2_0
  115. [Themeable (false)]
  116. #endif
  117. [DefaultValue("")]
  118. [WebSysDescription ("")]
  119. [WebCategory ("Data")]
  120. public virtual string DataTextField {
  121. get { return ViewState.GetString ("DataTextField", String.Empty); }
  122. set { ViewState ["DataTextField"] = value; }
  123. }
  124. #if NET_2_0
  125. [Themeable (false)]
  126. #endif
  127. [DefaultValue("")]
  128. [WebSysDescription ("")]
  129. [WebCategory ("Data")]
  130. public virtual string DataTextFormatString {
  131. get { return ViewState.GetString ("DataTextFormatString", String.Empty); }
  132. set { ViewState ["DataTextFormatString"] = value; }
  133. }
  134. #if NET_2_0
  135. [Themeable (false)]
  136. #endif
  137. [DefaultValue("")]
  138. [WebSysDescription ("")]
  139. [WebCategory ("Data")]
  140. public virtual string DataValueField {
  141. get { return ViewState.GetString ("DataValueField", String.Empty); }
  142. set { ViewState ["DataValueField"] = value; }
  143. }
  144. #if NET_2_0
  145. [Editor ("System.Web.UI.Design.WebControls.ListItemsCollectionEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  146. #endif
  147. [DefaultValue(null)]
  148. [MergableProperty(false)]
  149. [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
  150. [WebSysDescription ("")]
  151. [WebCategory ("Misc")]
  152. public virtual ListItemCollection Items {
  153. get {
  154. if (items == null)
  155. items = new ListItemCollection ();
  156. return items;
  157. }
  158. }
  159. // I can't find this info stored in the viewstate anywhere
  160. // so it must be calculated on the fly.
  161. [Bindable(true)]
  162. [Browsable(false)]
  163. [DefaultValue(0)]
  164. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  165. #if NET_2_0
  166. [Themeable (false)]
  167. #endif
  168. [WebSysDescription ("")]
  169. [WebCategory ("Misc")]
  170. public virtual int SelectedIndex {
  171. get {
  172. if (items == null)
  173. return -1;
  174. for (int i = 0; i < items.Count; i++) {
  175. if (items [i].Selected)
  176. return i;
  177. }
  178. return -1;
  179. }
  180. set {
  181. if (items == null || items.Count == 0) {
  182. // This will happen when assigning this property
  183. // before DataBind () is called on the control.
  184. saved_selected_index = value;
  185. return;
  186. }
  187. if (value < -1 || value >= Items.Count)
  188. throw new ArgumentOutOfRangeException ("value");
  189. ClearSelection ();
  190. if (value == -1)
  191. return;
  192. items [value].Selected = true;
  193. /* you'd think this would be called, but noooo */
  194. //OnSelectedIndexChanged (EventArgs.Empty);
  195. }
  196. }
  197. [Browsable(false)]
  198. [DefaultValue(null)]
  199. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  200. [WebSysDescription ("")]
  201. [WebCategory ("Misc")]
  202. public virtual ListItem SelectedItem {
  203. get {
  204. int si = SelectedIndex;
  205. if (si == -1)
  206. return null;
  207. return Items [si];
  208. }
  209. }
  210. #if NET_2_0
  211. [Bindable(true, BindingDirection.TwoWay)]
  212. [Themeable (false)]
  213. #else
  214. [Bindable(true)]
  215. #endif
  216. [Browsable(false)]
  217. [DefaultValue("")]
  218. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  219. [WebSysDescription ("")]
  220. [WebCategory ("Misc")]
  221. public virtual string SelectedValue {
  222. get {
  223. int si = SelectedIndex;
  224. if (si == -1)
  225. return String.Empty;
  226. return Items [si].Value;
  227. }
  228. set {
  229. ClearSelection ();
  230. if (items == null || items.Count == 0) {
  231. // This will happen when assigning this property
  232. // before DataBind () is called on the control.
  233. saved_selected_value = value;
  234. return;
  235. }
  236. int count = Items.Count;
  237. ListItemCollection coll = Items;
  238. bool thr = true;
  239. for (int i = 0; i < count; i++) {
  240. if (coll [i].Value == value) {
  241. coll [i].Selected = true;
  242. thr = false;
  243. }
  244. }
  245. if (thr) {
  246. string msg = String.Format ("Argument value is out of range: {0}", value);
  247. throw new ArgumentOutOfRangeException (msg);
  248. }
  249. }
  250. }
  251. #if NET_2_0
  252. [Themeable (false)]
  253. [DefaultValue ("")]
  254. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  255. [Browsable (false)]
  256. [MonoTODO]
  257. [WebSysDescription ("")]
  258. [WebCategoryAttribute ("Behavior")]
  259. public virtual string Text {
  260. get {
  261. return SelectedValue;
  262. }
  263. set {
  264. SelectedValue = value;
  265. /* you'd think this would be called, but noooo */
  266. //OnTextChanged (EventArgs.Empty);
  267. }
  268. }
  269. #if HAVE_CONTROL_ADAPTERS
  270. protected virtual new
  271. #else
  272. protected override
  273. #endif
  274. HtmlTextWriterTag TagKey
  275. {
  276. get {
  277. return HtmlTextWriterTag.Select;
  278. }
  279. }
  280. protected override void AddAttributesToRender (HtmlTextWriter w)
  281. {
  282. base.AddAttributesToRender (w);
  283. }
  284. #endif
  285. public virtual void ClearSelection ()
  286. {
  287. if (items == null)
  288. return;
  289. int count = Items.Count;
  290. for (int i = 0; i<count; i++)
  291. items [i].Selected = false;
  292. }
  293. #if NET_2_0
  294. protected internal override void LoadControlState (object savedState)
  295. {
  296. object first = null;
  297. ArrayList indices = null;
  298. Pair pair = savedState as Pair;
  299. if (pair != null) {
  300. first = pair.First;
  301. indices = pair.Second as ArrayList;
  302. }
  303. base.LoadControlState (first);
  304. if (indices != null) {
  305. int count = Items.Count;
  306. foreach (int index in indices) {
  307. if (index >= 0 && index < count)
  308. Items [index].Selected = true;
  309. }
  310. }
  311. }
  312. #endif
  313. protected override void OnDataBinding (EventArgs e)
  314. {
  315. base.OnDataBinding (e);
  316. IEnumerable list = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
  317. if (list == null)
  318. return;
  319. #if NET_2_0
  320. if (!AppendDataBoundItems)
  321. #endif
  322. Items.Clear();
  323. #if !NET_2_0
  324. DoDataBinding (list);
  325. #endif
  326. if (saved_selected_value != null) {
  327. SelectedValue = saved_selected_value;
  328. if (saved_selected_index != -2 && saved_selected_index != SelectedIndex)
  329. throw new ArgumentException ("SelectedIndex and SelectedValue are mutually exclusive.");
  330. } else if (saved_selected_index != -2) {
  331. SelectedIndex = saved_selected_index;
  332. // No need to check saved_selected_value here, as it's done before.
  333. }
  334. }
  335. #if NET_2_0
  336. protected internal override void OnInit (EventArgs e)
  337. {
  338. Page.RegisterRequiresControlState (this);
  339. base.OnInit (e);
  340. }
  341. #endif
  342. #if NET_2_0
  343. protected internal
  344. #else
  345. protected
  346. #endif
  347. override void OnPreRender (EventArgs e)
  348. {
  349. base.OnPreRender (e);
  350. }
  351. void DoDataBinding (IEnumerable dataSource)
  352. {
  353. if (dataSource != null) {
  354. string format = DataTextFormatString;
  355. if (format == "")
  356. format = null;
  357. string text_field = DataTextField;
  358. string value_field = DataValueField;
  359. ListItemCollection coll = Items;
  360. foreach (object container in dataSource) {
  361. string text;
  362. string val;
  363. text = val = null;
  364. if (text_field != "") {
  365. text = DataBinder.GetPropertyValue (container, text_field, format);
  366. }
  367. if (value_field != "") {
  368. val = DataBinder.GetPropertyValue (container, value_field).ToString ();
  369. } else if (text_field == "") {
  370. text = val = container.ToString ();
  371. if (format != null)
  372. text = String.Format (format, container);
  373. } else if (text != null) {
  374. val = text;
  375. }
  376. if (text == null)
  377. text = val;
  378. coll.Add (new ListItem (text, val));
  379. }
  380. }
  381. }
  382. #if NET_2_0
  383. protected virtual void OnTextChanged (EventArgs e)
  384. {
  385. EventHandler handler = (EventHandler) Events [TextChangedEvent];
  386. if (handler != null)
  387. handler (this, e);
  388. }
  389. protected internal override void PerformDataBinding (IEnumerable dataSource)
  390. {
  391. base.PerformDataBinding (dataSource);
  392. DoDataBinding (dataSource);
  393. }
  394. [MonoTODO ("why override?")]
  395. protected override void PerformSelect ()
  396. {
  397. base.PerformSelect ();
  398. }
  399. [MonoTODO]
  400. protected internal override void RenderContents (HtmlTextWriter w)
  401. {
  402. base.RenderContents (w);
  403. }
  404. protected internal override object SaveControlState ()
  405. {
  406. object first;
  407. ArrayList second;
  408. first = base.SaveControlState ();
  409. second = GetSelectedIndicesInternal ();
  410. if (second == null)
  411. second = new ArrayList();
  412. return new Pair (first, second);
  413. }
  414. #endif
  415. internal ArrayList GetSelectedIndicesInternal ()
  416. {
  417. ArrayList selected = null;
  418. if (items != null) {
  419. selected = new ArrayList ();
  420. int count = Items.Count;
  421. for (int i = 0; i < count; i++) {
  422. if (items [i].Selected)
  423. selected.Add (i);
  424. }
  425. }
  426. return selected;
  427. }
  428. protected override object SaveViewState ()
  429. {
  430. object first = null;
  431. object second = null;
  432. first = base.SaveViewState ();
  433. IStateManager manager = items as IStateManager;
  434. if (manager != null)
  435. second = manager.SaveViewState ();
  436. #if !NET_2_0
  437. ArrayList selected = GetSelectedIndicesInternal ();
  438. #endif
  439. if (first == null && second == null
  440. #if !NET_2_0
  441. && selected == null
  442. #endif
  443. )
  444. return null;
  445. #if NET_2_0
  446. return new Pair (first, second);
  447. #else
  448. return new Triplet (first, second, selected);
  449. #endif
  450. }
  451. protected override void LoadViewState (object savedState)
  452. {
  453. object first = null;
  454. object second = null;
  455. #if !NET_2_0
  456. ArrayList indices = null;
  457. #endif
  458. #if NET_2_0
  459. Pair pair = savedState as Pair;
  460. if (pair != null) {
  461. first = pair.First;
  462. second = pair.Second;
  463. }
  464. #else
  465. Triplet triplet = savedState as Triplet;
  466. if (triplet != null) {
  467. first = triplet.First;
  468. second = triplet.Second;
  469. indices = triplet.Third as ArrayList;
  470. }
  471. #endif
  472. base.LoadViewState (first);
  473. if (second != null) {
  474. IStateManager manager = Items as IStateManager;
  475. manager.LoadViewState (second);
  476. }
  477. #if !NET_2_0
  478. if (indices != null) {
  479. foreach (int index in indices)
  480. Items [index].Selected = true;
  481. }
  482. #endif
  483. }
  484. #if NET_2_0
  485. [MonoTODO]
  486. protected void SetPostDataSelection (int selectedIndex)
  487. {
  488. throw new NotImplementedException ();
  489. }
  490. #endif
  491. protected override void TrackViewState ()
  492. {
  493. base.TrackViewState ();
  494. IStateManager manager = items as IStateManager;
  495. if (manager != null)
  496. manager.TrackViewState ();
  497. }
  498. protected virtual void OnSelectedIndexChanged (EventArgs e)
  499. {
  500. EventHandler handler = (EventHandler) Events [SelectedIndexChangedEvent];
  501. if (handler != null)
  502. handler (this, e);
  503. }
  504. #if NET_2_0
  505. protected internal virtual void VerifyMultiSelect ()
  506. {
  507. }
  508. #endif
  509. [WebSysDescription ("")]
  510. [WebCategory ("Action")]
  511. public event EventHandler SelectedIndexChanged {
  512. add { Events.AddHandler (SelectedIndexChangedEvent, value); }
  513. remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
  514. }
  515. #if NET_2_0
  516. /* sealed in the docs */
  517. public event EventHandler TextChanged {
  518. add {
  519. Events.AddHandler (TextChangedEvent, value);
  520. }
  521. remove {
  522. Events.RemoveHandler (TextChangedEvent, value);
  523. }
  524. }
  525. [MonoTODO]
  526. [Themeable (false)]
  527. [DefaultValue (false)]
  528. [WebSysDescription ("")]
  529. [WebCategory ("Behavior")]
  530. public virtual bool CausesValidation {
  531. get {
  532. throw new NotImplementedException ();
  533. }
  534. set {
  535. throw new NotImplementedException ();
  536. }
  537. }
  538. [MonoTODO]
  539. [Themeable (false)]
  540. [DefaultValue ("")]
  541. [WebSysDescription ("")]
  542. [WebCategoryAttribute ("Behavior")]
  543. public virtual string ValidationGroup {
  544. get {
  545. throw new NotImplementedException ();
  546. }
  547. set {
  548. throw new NotImplementedException ();
  549. }
  550. }
  551. #endif
  552. }
  553. }