ListControl.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. for (int i = 0; i < count; i++) {
  235. if (coll [i].Value == value)
  236. coll [i].Selected = true;
  237. }
  238. }
  239. }
  240. #if NET_2_0
  241. [Themeable (false)]
  242. [DefaultValue ("")]
  243. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  244. [Browsable (false)]
  245. [MonoTODO]
  246. [WebSysDescription ("")]
  247. [WebCategoryAttribute ("Behavior")]
  248. public virtual string Text {
  249. get {
  250. return SelectedValue;
  251. }
  252. set {
  253. SelectedValue = value;
  254. /* you'd think this would be called, but noooo */
  255. //OnTextChanged (EventArgs.Empty);
  256. }
  257. }
  258. #if HAVE_CONTROL_ADAPTERS
  259. protected virtual new
  260. #else
  261. protected override
  262. #endif
  263. HtmlTextWriterTag TagKey
  264. {
  265. get {
  266. return HtmlTextWriterTag.Select;
  267. }
  268. }
  269. protected override void AddAttributesToRender (HtmlTextWriter w)
  270. {
  271. base.AddAttributesToRender (w);
  272. }
  273. #endif
  274. public virtual void ClearSelection ()
  275. {
  276. if (items == null)
  277. return;
  278. int count = Items.Count;
  279. for (int i = 0; i<count; i++)
  280. items [i].Selected = false;
  281. }
  282. #if NET_2_0
  283. protected internal override void LoadControlState (object savedState)
  284. {
  285. object first = null;
  286. ArrayList indices = null;
  287. Pair pair = savedState as Pair;
  288. if (pair != null) {
  289. first = pair.First;
  290. indices = pair.Second as ArrayList;
  291. }
  292. base.LoadControlState (first);
  293. if (indices != null) {
  294. foreach (int index in indices)
  295. Items [index].Selected = true;
  296. }
  297. }
  298. #endif
  299. protected override void OnDataBinding (EventArgs e)
  300. {
  301. base.OnDataBinding (e);
  302. IEnumerable list = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
  303. if (list == null)
  304. return;
  305. Items.Clear ();
  306. string format = DataTextFormatString;
  307. if (format == "")
  308. format = null;
  309. string text_field = DataTextField;
  310. string value_field = DataValueField;
  311. ListItemCollection coll = Items;
  312. foreach (object container in list) {
  313. string text;
  314. string val;
  315. if (text_field != "") {
  316. text = DataBinder.GetPropertyValue (container, text_field, format);
  317. } else {
  318. text = "";
  319. }
  320. if (value_field != "") {
  321. val = DataBinder.GetPropertyValue (container, value_field).ToString ();
  322. } else if (text_field == "") {
  323. text = val = container.ToString ();
  324. if (format != null)
  325. text = String.Format (format, container);
  326. } else {
  327. val = text;
  328. }
  329. coll.Add (new ListItem (text, val));
  330. }
  331. if (saved_selected_value != null) {
  332. SelectedValue = saved_selected_value;
  333. if (saved_selected_index != -2 && saved_selected_index != SelectedIndex)
  334. throw new ArgumentException ("SelectedIndex and SelectedValue are mutually exclusive.");
  335. } else if (saved_selected_index != -2) {
  336. SelectedIndex = saved_selected_index;
  337. // No need to check saved_selected_value here, as it's done before.
  338. }
  339. }
  340. #if NET_2_0
  341. protected internal override void OnInit (EventArgs e)
  342. {
  343. Page.RegisterRequiresControlState (this);
  344. base.OnInit (e);
  345. }
  346. #endif
  347. #if NET_2_0
  348. protected internal
  349. #else
  350. protected
  351. #endif
  352. override void OnPreRender (EventArgs e)
  353. {
  354. base.OnPreRender (e);
  355. }
  356. #if NET_2_0
  357. protected virtual void OnTextChanged (EventArgs e)
  358. {
  359. EventHandler handler = (EventHandler) Events [TextChangedEvent];
  360. if (handler != null)
  361. handler (this, e);
  362. }
  363. [MonoTODO]
  364. protected internal override void PerformDataBinding (IEnumerable dataSource)
  365. {
  366. throw new NotImplementedException ();
  367. }
  368. [MonoTODO]
  369. protected override void PerformSelect ()
  370. {
  371. throw new NotImplementedException ();
  372. }
  373. [MonoTODO]
  374. protected internal override void RenderContents (HtmlTextWriter w)
  375. {
  376. base.RenderContents (w);
  377. }
  378. protected internal override object SaveControlState ()
  379. {
  380. object first;
  381. ArrayList second;
  382. first = base.SaveControlState ();
  383. second = GetSelectedIndices();
  384. if (second == null)
  385. second = new ArrayList();
  386. return new Pair (first, second);
  387. }
  388. #endif
  389. ArrayList GetSelectedIndices ()
  390. {
  391. ArrayList selected = null;
  392. if (items != null) {
  393. selected = new ArrayList ();
  394. int count = Items.Count;
  395. for (int i = 0; i < count; i++) {
  396. if (items [i].Selected)
  397. selected.Add (i);
  398. }
  399. }
  400. return selected;
  401. }
  402. protected override object SaveViewState ()
  403. {
  404. object first = null;
  405. object second = null;
  406. first = base.SaveViewState ();
  407. IStateManager manager = items as IStateManager;
  408. if (manager != null)
  409. second = manager.SaveViewState ();
  410. #if !NET_2_0
  411. ArrayList selected = GetSelectedIndices ();
  412. #endif
  413. if (first == null && second == null
  414. #if !NET_2_0
  415. && selected == null
  416. #endif
  417. )
  418. return null;
  419. #if NET_2_0
  420. return new Pair (first, second);
  421. #else
  422. return new Triplet (first, second, selected);
  423. #endif
  424. }
  425. protected override void LoadViewState (object savedState)
  426. {
  427. object first = null;
  428. object second = null;
  429. #if !NET_2_0
  430. ArrayList indices = null;
  431. #endif
  432. #if NET_2_0
  433. Pair pair = savedState as Pair;
  434. if (pair != null) {
  435. first = pair.First;
  436. second = pair.Second;
  437. }
  438. #else
  439. Triplet triplet = savedState as Triplet;
  440. if (triplet != null) {
  441. first = triplet.First;
  442. second = triplet.Second;
  443. indices = triplet.Third as ArrayList;
  444. }
  445. #endif
  446. base.LoadViewState (first);
  447. if (second != null) {
  448. IStateManager manager = Items as IStateManager;
  449. manager.LoadViewState (second);
  450. }
  451. #if !NET_2_0
  452. if (indices != null) {
  453. foreach (int index in indices)
  454. Items [index].Selected = true;
  455. }
  456. #endif
  457. }
  458. #if NET_2_0
  459. [MonoTODO]
  460. protected void SetPostDataSelection (int selectedIndex)
  461. {
  462. throw new NotImplementedException ();
  463. }
  464. #endif
  465. protected override void TrackViewState ()
  466. {
  467. base.TrackViewState ();
  468. IStateManager manager = items as IStateManager;
  469. if (manager != null)
  470. manager.TrackViewState ();
  471. }
  472. protected virtual void OnSelectedIndexChanged (EventArgs e)
  473. {
  474. EventHandler handler = (EventHandler) Events [SelectedIndexChangedEvent];
  475. if (handler != null)
  476. handler (this, e);
  477. }
  478. #if NET_2_0
  479. protected internal virtual void VerifyMultiSelect ()
  480. {
  481. }
  482. #endif
  483. [WebSysDescription ("")]
  484. [WebCategory ("Action")]
  485. public event EventHandler SelectedIndexChanged {
  486. add { Events.AddHandler (SelectedIndexChangedEvent, value); }
  487. remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
  488. }
  489. #if NET_2_0
  490. /* sealed in the docs */
  491. public event EventHandler TextChanged {
  492. add {
  493. Events.AddHandler (TextChangedEvent, value);
  494. }
  495. remove {
  496. Events.RemoveHandler (TextChangedEvent, value);
  497. }
  498. }
  499. [MonoTODO]
  500. [Themeable (false)]
  501. [DefaultValue (false)]
  502. [WebSysDescription ("")]
  503. [WebCategory ("Behavior")]
  504. public virtual bool CausesValidation {
  505. get {
  506. throw new NotImplementedException ();
  507. }
  508. set {
  509. throw new NotImplementedException ();
  510. }
  511. }
  512. [MonoTODO]
  513. [Themeable (false)]
  514. [DefaultValue ("")]
  515. [WebSysDescription ("")]
  516. [WebCategoryAttribute ("Behavior")]
  517. public virtual string ValidationGroup {
  518. get {
  519. throw new NotImplementedException ();
  520. }
  521. set {
  522. throw new NotImplementedException ();
  523. }
  524. }
  525. #endif
  526. }
  527. }