ListControl.cs 13 KB

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