ListControl.cs 14 KB

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