ListControl.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. protected override void OnDataBinding (EventArgs e)
  294. {
  295. base.OnDataBinding (e);
  296. #if !NET_2_0
  297. IEnumerable list = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
  298. if (list == null)
  299. return;
  300. #endif
  301. #if NET_2_0
  302. if (!AppendDataBoundItems)
  303. #endif
  304. Items.Clear();
  305. #if !NET_2_0
  306. DoDataBinding (list);
  307. #endif
  308. }
  309. #if NET_2_0
  310. protected internal
  311. #else
  312. protected
  313. #endif
  314. override void OnPreRender (EventArgs e)
  315. {
  316. base.OnPreRender (e);
  317. }
  318. void DoDataBinding (IEnumerable dataSource)
  319. {
  320. if (dataSource != null) {
  321. string format = DataTextFormatString;
  322. if (format == "")
  323. format = null;
  324. string text_field = DataTextField;
  325. string value_field = DataValueField;
  326. ListItemCollection coll = Items;
  327. foreach (object container in dataSource) {
  328. string text;
  329. string val;
  330. text = val = null;
  331. if (text_field != "") {
  332. text = DataBinder.GetPropertyValue (container, text_field, format);
  333. }
  334. if (value_field != "") {
  335. val = DataBinder.GetPropertyValue (container, value_field).ToString ();
  336. } else if (text_field == "") {
  337. text = val = container.ToString ();
  338. if (format != null)
  339. text = String.Format (format, container);
  340. } else if (text != null) {
  341. val = text;
  342. }
  343. if (text == null)
  344. text = val;
  345. coll.Add (new ListItem (text, val));
  346. }
  347. }
  348. if (saved_selected_value != null) {
  349. SelectedValue = saved_selected_value;
  350. if (saved_selected_index != -2 && saved_selected_index != SelectedIndex)
  351. throw new ArgumentException ("SelectedIndex and SelectedValue are mutually exclusive.");
  352. }
  353. else if (saved_selected_index != -2) {
  354. SelectedIndex = saved_selected_index;
  355. // No need to check saved_selected_value here, as it's done before.
  356. }
  357. }
  358. #if NET_2_0
  359. protected virtual void OnTextChanged (EventArgs e)
  360. {
  361. EventHandler handler = (EventHandler) Events [TextChangedEvent];
  362. if (handler != null)
  363. handler (this, e);
  364. }
  365. protected internal override void PerformDataBinding (IEnumerable dataSource)
  366. {
  367. base.PerformDataBinding (dataSource);
  368. DoDataBinding (dataSource);
  369. }
  370. [MonoTODO ("why override?")]
  371. protected override void PerformSelect ()
  372. {
  373. base.PerformSelect ();
  374. }
  375. [MonoTODO]
  376. protected internal override void RenderContents (HtmlTextWriter w)
  377. {
  378. base.RenderContents (w);
  379. }
  380. #endif
  381. internal ArrayList GetSelectedIndicesInternal ()
  382. {
  383. ArrayList selected = null;
  384. if (items != null) {
  385. selected = new ArrayList ();
  386. int count = Items.Count;
  387. for (int i = 0; i < count; i++) {
  388. if (items [i].Selected)
  389. selected.Add (i);
  390. }
  391. }
  392. return selected;
  393. }
  394. protected override object SaveViewState ()
  395. {
  396. object first = null;
  397. object second = null;
  398. first = base.SaveViewState ();
  399. IStateManager manager = items as IStateManager;
  400. if (manager != null)
  401. second = manager.SaveViewState ();
  402. ArrayList selected = GetSelectedIndicesInternal ();
  403. if (first == null && second == null && selected == null)
  404. return null;
  405. return new Triplet (first, second, selected);
  406. }
  407. protected override void LoadViewState (object savedState)
  408. {
  409. object first = null;
  410. object second = null;
  411. ArrayList indices = null;
  412. Triplet triplet = savedState as Triplet;
  413. if (triplet != null) {
  414. first = triplet.First;
  415. second = triplet.Second;
  416. indices = triplet.Third as ArrayList;
  417. }
  418. base.LoadViewState (first);
  419. if (second != null) {
  420. IStateManager manager = Items as IStateManager;
  421. manager.LoadViewState (second);
  422. }
  423. if (indices != null) {
  424. foreach (int index in indices)
  425. Items [index].Selected = true;
  426. }
  427. }
  428. #if NET_2_0
  429. [MonoTODO]
  430. protected void SetPostDataSelection (int selectedIndex)
  431. {
  432. throw new NotImplementedException ();
  433. }
  434. #endif
  435. protected override void TrackViewState ()
  436. {
  437. base.TrackViewState ();
  438. IStateManager manager = items as IStateManager;
  439. if (manager != null)
  440. manager.TrackViewState ();
  441. }
  442. protected virtual void OnSelectedIndexChanged (EventArgs e)
  443. {
  444. EventHandler handler = (EventHandler) Events [SelectedIndexChangedEvent];
  445. if (handler != null)
  446. handler (this, e);
  447. }
  448. #if NET_2_0
  449. protected internal virtual void VerifyMultiSelect ()
  450. {
  451. }
  452. #endif
  453. [WebSysDescription ("")]
  454. [WebCategory ("Action")]
  455. public event EventHandler SelectedIndexChanged {
  456. add { Events.AddHandler (SelectedIndexChangedEvent, value); }
  457. remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
  458. }
  459. #if NET_2_0
  460. /* sealed in the docs */
  461. public event EventHandler TextChanged {
  462. add {
  463. Events.AddHandler (TextChangedEvent, value);
  464. }
  465. remove {
  466. Events.RemoveHandler (TextChangedEvent, value);
  467. }
  468. }
  469. [Themeable (false)]
  470. [DefaultValue (false)]
  471. [WebSysDescription ("")]
  472. [WebCategory ("Behavior")]
  473. public virtual bool CausesValidation {
  474. get {
  475. return ViewState.GetBool ("CausesValidation", false);
  476. }
  477. set {
  478. ViewState ["CausesValidation"] = value;
  479. }
  480. }
  481. [Themeable (false)]
  482. [DefaultValue ("")]
  483. [WebSysDescription ("")]
  484. [WebCategoryAttribute ("Behavior")]
  485. public virtual string ValidationGroup {
  486. get {
  487. return ViewState.GetString ("ValidationGroup", "");
  488. }
  489. set {
  490. ViewState ["ValidationGroup"] = value;
  491. }
  492. }
  493. #endif
  494. }
  495. }