ListControl.cs 15 KB

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