ListControl.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 NET_2_0
  188. if (value >= Items.Count) {
  189. ClearSelection ();
  190. return;
  191. }
  192. #endif
  193. if (value < -1 || value >= Items.Count)
  194. throw new ArgumentOutOfRangeException ("value");
  195. ClearSelection ();
  196. if (value == -1)
  197. return;
  198. items [value].Selected = true;
  199. /* you'd think this would be called, but noooo */
  200. //OnSelectedIndexChanged (EventArgs.Empty);
  201. }
  202. }
  203. [Browsable(false)]
  204. [DefaultValue(null)]
  205. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  206. [WebSysDescription ("")]
  207. [WebCategory ("Misc")]
  208. public virtual ListItem SelectedItem {
  209. get {
  210. int si = SelectedIndex;
  211. if (si == -1)
  212. return null;
  213. return Items [si];
  214. }
  215. }
  216. #if NET_2_0
  217. [Bindable(true, BindingDirection.TwoWay)]
  218. [Themeable (false)]
  219. #else
  220. [Bindable(true)]
  221. #endif
  222. [Browsable(false)]
  223. [DefaultValue("")]
  224. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  225. [WebSysDescription ("")]
  226. [WebCategory ("Misc")]
  227. public virtual string SelectedValue {
  228. get {
  229. int si = SelectedIndex;
  230. if (si == -1)
  231. return String.Empty;
  232. return Items [si].Value;
  233. }
  234. set {
  235. ClearSelection ();
  236. if (items == null || items.Count == 0) {
  237. // This will happen when assigning this property
  238. // before DataBind () is called on the control.
  239. saved_selected_value = value;
  240. return;
  241. }
  242. int count = Items.Count;
  243. ListItemCollection coll = Items;
  244. bool thr = true;
  245. for (int i = 0; i < count; i++) {
  246. if (coll [i].Value == value) {
  247. coll [i].Selected = true;
  248. thr = false;
  249. }
  250. }
  251. #if !NET_2_0
  252. if (thr) {
  253. string msg = String.Format ("Argument value is out of range: {0}", value);
  254. throw new ArgumentOutOfRangeException (msg);
  255. }
  256. #endif
  257. }
  258. }
  259. #if NET_2_0
  260. [Themeable (false)]
  261. [DefaultValue ("")]
  262. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  263. [Browsable (false)]
  264. [MonoTODO]
  265. [WebSysDescription ("")]
  266. [WebCategoryAttribute ("Behavior")]
  267. public virtual string Text {
  268. get {
  269. return SelectedValue;
  270. }
  271. set {
  272. SelectedValue = value;
  273. /* you'd think this would be called, but noooo */
  274. //OnTextChanged (EventArgs.Empty);
  275. }
  276. }
  277. #if HAVE_CONTROL_ADAPTERS
  278. protected virtual new
  279. #else
  280. protected override
  281. #endif
  282. HtmlTextWriterTag TagKey
  283. {
  284. get {
  285. return HtmlTextWriterTag.Select;
  286. }
  287. }
  288. protected override void AddAttributesToRender (HtmlTextWriter w)
  289. {
  290. base.AddAttributesToRender (w);
  291. }
  292. #endif
  293. public virtual void ClearSelection ()
  294. {
  295. if (items == null)
  296. return;
  297. int count = Items.Count;
  298. for (int i = 0; i<count; i++)
  299. items [i].Selected = false;
  300. }
  301. protected override void OnDataBinding (EventArgs e)
  302. {
  303. base.OnDataBinding (e);
  304. #if !NET_2_0
  305. IEnumerable list = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
  306. if (list == null)
  307. return;
  308. DoDataBinding (list);
  309. #endif
  310. }
  311. #if NET_2_0
  312. protected internal
  313. #else
  314. protected
  315. #endif
  316. override void OnPreRender (EventArgs e)
  317. {
  318. base.OnPreRender (e);
  319. }
  320. void DoDataBinding (IEnumerable dataSource)
  321. {
  322. if (dataSource != null) {
  323. #if NET_2_0
  324. if (!AppendDataBoundItems)
  325. #endif
  326. Items.Clear ();
  327. string format = DataTextFormatString;
  328. if (format == "")
  329. format = null;
  330. string text_field = DataTextField;
  331. string value_field = DataValueField;
  332. ListItemCollection coll = Items;
  333. foreach (object container in dataSource) {
  334. string text;
  335. string val;
  336. text = val = null;
  337. if (text_field != "") {
  338. text = DataBinder.GetPropertyValue (container, text_field, format);
  339. }
  340. if (value_field != "") {
  341. val = DataBinder.GetPropertyValue (container, value_field).ToString ();
  342. } else if (text_field == "") {
  343. text = val = container.ToString ();
  344. if (format != null)
  345. text = String.Format (format, container);
  346. } else if (text != null) {
  347. val = text;
  348. }
  349. if (text == null)
  350. text = val;
  351. coll.Add (new ListItem (text, val));
  352. }
  353. }
  354. if (saved_selected_value != null) {
  355. SelectedValue = saved_selected_value;
  356. if (saved_selected_index != -2 && saved_selected_index != SelectedIndex)
  357. throw new ArgumentException ("SelectedIndex and SelectedValue are mutually exclusive.");
  358. }
  359. else if (saved_selected_index != -2) {
  360. SelectedIndex = saved_selected_index;
  361. // No need to check saved_selected_value here, as it's done before.
  362. }
  363. }
  364. #if NET_2_0
  365. protected virtual void OnTextChanged (EventArgs e)
  366. {
  367. EventHandler handler = (EventHandler) Events [TextChangedEvent];
  368. if (handler != null)
  369. handler (this, e);
  370. }
  371. protected internal override void PerformDataBinding (IEnumerable dataSource)
  372. {
  373. base.PerformDataBinding (dataSource);
  374. if (dataSource == null)
  375. return;
  376. DoDataBinding (dataSource);
  377. }
  378. [MonoTODO ("why override?")]
  379. protected override void PerformSelect ()
  380. {
  381. base.PerformSelect ();
  382. }
  383. [MonoTODO]
  384. protected internal override void RenderContents (HtmlTextWriter w)
  385. {
  386. base.RenderContents (w);
  387. }
  388. #endif
  389. internal ArrayList GetSelectedIndicesInternal ()
  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. ArrayList selected = GetSelectedIndicesInternal ();
  411. if (first == null && second == null && selected == null)
  412. return null;
  413. return new Triplet (first, second, selected);
  414. }
  415. protected override void LoadViewState (object savedState)
  416. {
  417. object first = null;
  418. object second = null;
  419. ArrayList indices = null;
  420. Triplet triplet = savedState as Triplet;
  421. if (triplet != null) {
  422. first = triplet.First;
  423. second = triplet.Second;
  424. indices = triplet.Third as ArrayList;
  425. }
  426. base.LoadViewState (first);
  427. if (second != null) {
  428. IStateManager manager = Items as IStateManager;
  429. manager.LoadViewState (second);
  430. }
  431. if (indices != null) {
  432. foreach (int index in indices)
  433. Items [index].Selected = true;
  434. }
  435. }
  436. #if NET_2_0
  437. [MonoTODO]
  438. protected void SetPostDataSelection (int selectedIndex)
  439. {
  440. throw new NotImplementedException ();
  441. }
  442. #endif
  443. protected override void TrackViewState ()
  444. {
  445. base.TrackViewState ();
  446. IStateManager manager = items as IStateManager;
  447. if (manager != null)
  448. manager.TrackViewState ();
  449. }
  450. protected virtual void OnSelectedIndexChanged (EventArgs e)
  451. {
  452. EventHandler handler = (EventHandler) Events [SelectedIndexChangedEvent];
  453. if (handler != null)
  454. handler (this, e);
  455. }
  456. #if NET_2_0
  457. protected internal virtual void VerifyMultiSelect ()
  458. {
  459. }
  460. #endif
  461. [WebSysDescription ("")]
  462. [WebCategory ("Action")]
  463. public event EventHandler SelectedIndexChanged {
  464. add { Events.AddHandler (SelectedIndexChangedEvent, value); }
  465. remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
  466. }
  467. #if NET_2_0
  468. /* sealed in the docs */
  469. public event EventHandler TextChanged {
  470. add {
  471. Events.AddHandler (TextChangedEvent, value);
  472. }
  473. remove {
  474. Events.RemoveHandler (TextChangedEvent, value);
  475. }
  476. }
  477. [Themeable (false)]
  478. [DefaultValue (false)]
  479. [WebSysDescription ("")]
  480. [WebCategory ("Behavior")]
  481. public virtual bool CausesValidation {
  482. get {
  483. return ViewState.GetBool ("CausesValidation", false);
  484. }
  485. set {
  486. ViewState ["CausesValidation"] = value;
  487. }
  488. }
  489. [Themeable (false)]
  490. [DefaultValue ("")]
  491. [WebSysDescription ("")]
  492. [WebCategoryAttribute ("Behavior")]
  493. public virtual string ValidationGroup {
  494. get {
  495. return ViewState.GetString ("ValidationGroup", "");
  496. }
  497. set {
  498. ViewState ["ValidationGroup"] = value;
  499. }
  500. }
  501. #endif
  502. }
  503. }