HtmlSelect.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. //
  2. // System.Web.UI.HtmlControls.HtmlSelect.cs
  3. //
  4. // Author:
  5. // Dick Porter <[email protected]>
  6. //
  7. // Copyright (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.Web.UI;
  29. using System.Web.UI.WebControls;
  30. using System.Web.Util;
  31. using System.ComponentModel;
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Globalization;
  35. namespace System.Web.UI.HtmlControls
  36. {
  37. [DefaultEvent ("ServerChange")]
  38. [ValidationProperty ("Value")]
  39. [ControlBuilder (typeof (HtmlSelectBuilder))]
  40. public class HtmlSelect : HtmlContainerControl , IPostBackDataHandler
  41. #if NET_2_0
  42. , IParserAccessor
  43. #endif
  44. {
  45. public HtmlSelect () : base ("select")
  46. {
  47. }
  48. [DefaultValue ("")]
  49. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  50. public virtual string DataMember
  51. {
  52. get {
  53. string member = Attributes["datamember"];
  54. if (member == null) {
  55. return (String.Empty);
  56. }
  57. return (member);
  58. }
  59. set {
  60. if (value == null) {
  61. Attributes.Remove ("datamember");
  62. } else {
  63. Attributes["datamember"] = value;
  64. }
  65. }
  66. }
  67. object datasource;
  68. [DefaultValue (null)]
  69. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  70. public virtual object DataSource
  71. {
  72. get {
  73. return (datasource);
  74. }
  75. set {
  76. if ((value != null) &&
  77. !(value is IEnumerable) &&
  78. !(value is IListSource)) {
  79. throw new ArgumentException ();
  80. }
  81. datasource = value;
  82. }
  83. }
  84. #if NET_2_0
  85. [DefaultValue ("")]
  86. public virtual string DataSourceID
  87. {
  88. get {
  89. return (String.Empty);
  90. }
  91. set {
  92. }
  93. }
  94. #endif
  95. [DefaultValue ("")]
  96. public virtual string DataTextField
  97. {
  98. get {
  99. string text = Attributes["datatextfield"];
  100. if (text == null) {
  101. return (String.Empty);
  102. }
  103. return (text);
  104. }
  105. set {
  106. if (value == null) {
  107. Attributes.Remove ("datatextfield");
  108. } else {
  109. Attributes["datatextfield"] = value;
  110. }
  111. }
  112. }
  113. [DefaultValue ("")]
  114. public virtual string DataValueField
  115. {
  116. get {
  117. string value = Attributes["datavaluefield"];
  118. if (value == null) {
  119. return (String.Empty);
  120. }
  121. return (value);
  122. }
  123. set {
  124. if (value == null) {
  125. Attributes.Remove ("datavaluefield");
  126. } else {
  127. Attributes["datavaluefield"] = value;
  128. }
  129. }
  130. }
  131. public override string InnerHtml
  132. {
  133. get {
  134. throw new NotSupportedException ();
  135. }
  136. set {
  137. throw new NotSupportedException ();
  138. }
  139. }
  140. public override string InnerText
  141. {
  142. get {
  143. throw new NotSupportedException ();
  144. }
  145. set {
  146. throw new NotSupportedException ();
  147. }
  148. }
  149. #if NET_2_0
  150. [MonoTODO]
  151. protected bool IsBoundUsingDataSourceID
  152. {
  153. get {
  154. throw new NotImplementedException ();
  155. }
  156. }
  157. #endif
  158. ListItemCollection items;
  159. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  160. [Browsable (false)]
  161. public ListItemCollection Items
  162. {
  163. get {
  164. if (items == null) {
  165. items = new ListItemCollection ();
  166. }
  167. return (items);
  168. }
  169. }
  170. [DefaultValue ("")]
  171. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  172. public bool Multiple
  173. {
  174. get {
  175. string multi = Attributes["multiple"];
  176. if (multi == null) {
  177. return (false);
  178. }
  179. return (true);
  180. }
  181. set {
  182. if (value == false) {
  183. Attributes.Remove ("multiple");
  184. } else {
  185. Attributes["multiple"] = "multiple";
  186. }
  187. }
  188. }
  189. [DefaultValue ("")]
  190. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  191. public string Name
  192. {
  193. get {
  194. return (UniqueID);
  195. }
  196. set {
  197. /* Do nothing */
  198. }
  199. }
  200. #if NET_2_0
  201. [MonoTODO]
  202. protected bool RequiresDataBinding
  203. {
  204. get {
  205. throw new NotImplementedException ();
  206. }
  207. set {
  208. throw new NotImplementedException ();
  209. }
  210. }
  211. #endif
  212. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  213. [Browsable (false)]
  214. public virtual int SelectedIndex
  215. {
  216. get {
  217. /* Make sure Items has been initialised */
  218. ListItemCollection listitems = Items;
  219. for (int i = 0; i < listitems.Count; i++) {
  220. if (listitems[i].Selected) {
  221. return (i);
  222. }
  223. }
  224. /* There is always a selected item in
  225. * non-multiple mode, if the size is
  226. * <= 1
  227. */
  228. if (!Multiple && Size <= 1) {
  229. /* Select the first item */
  230. if (listitems.Count > 0) {
  231. /* And make it stick
  232. * if there is
  233. * anything in the
  234. * list
  235. */
  236. listitems[0].Selected = true;
  237. }
  238. return (0);
  239. }
  240. return (-1);
  241. }
  242. set {
  243. ClearSelection ();
  244. if (value == -1 || items == null) {
  245. return;
  246. }
  247. if (value < 0 || value >= items.Count) {
  248. throw new ArgumentOutOfRangeException ("value");
  249. }
  250. items[value].Selected = true;
  251. }
  252. }
  253. /* "internal infrastructure" according to the docs,
  254. * but has some documentation in 2.0
  255. */
  256. protected virtual int[] SelectedIndices
  257. {
  258. get {
  259. ArrayList selected = new ArrayList ();
  260. int count = Items.Count;
  261. for (int i = 0; i < count; i++) {
  262. if (Items [i].Selected) {
  263. selected.Add (i);
  264. }
  265. }
  266. return ((int[])selected.ToArray (typeof (int)));
  267. }
  268. }
  269. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  270. public int Size
  271. {
  272. get {
  273. string size = Attributes["size"];
  274. if (size == null) {
  275. return (-1);
  276. }
  277. return (Int32.Parse (size, CultureInfo.InvariantCulture));
  278. }
  279. set {
  280. if (value == -1) {
  281. Attributes.Remove ("size");
  282. } else {
  283. Attributes["size"] = value.ToString ();
  284. }
  285. }
  286. }
  287. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  288. public string Value
  289. {
  290. get {
  291. int sel = SelectedIndex;
  292. if (sel >= 0 && sel < Items.Count) {
  293. return (Items[sel].Value);
  294. }
  295. return (String.Empty);
  296. }
  297. set {
  298. int sel = Items.IndexOf (value);
  299. if (sel >= 0) {
  300. SelectedIndex = sel;
  301. }
  302. }
  303. }
  304. private static readonly object EventServerChange = new object ();
  305. public event EventHandler ServerChange
  306. {
  307. add {
  308. Events.AddHandler (EventServerChange, value);
  309. }
  310. remove {
  311. Events.RemoveHandler (EventServerChange, value);
  312. }
  313. }
  314. protected override void AddParsedSubObject (object obj)
  315. {
  316. if (!(obj is ListItem)) {
  317. throw new HttpException ("HtmlSelect can only contain ListItem");
  318. }
  319. Items.Add ((ListItem)obj);
  320. base.AddParsedSubObject (obj);
  321. }
  322. /* "internal infrastructure" according to the docs,
  323. * but has some documentation in 2.0
  324. */
  325. protected virtual void ClearSelection ()
  326. {
  327. if (items == null) {
  328. return;
  329. }
  330. int count = items.Count;
  331. for (int i = 0; i < count; i++) {
  332. items[i].Selected = false;
  333. }
  334. }
  335. protected override ControlCollection CreateControlCollection ()
  336. {
  337. return (base.CreateControlCollection ());
  338. }
  339. #if NET_2_0
  340. [MonoTODO]
  341. protected void EnsureDataBound ()
  342. {
  343. throw new NotImplementedException ();
  344. }
  345. [MonoTODO]
  346. protected virtual IEnumerable GetData ()
  347. {
  348. throw new NotImplementedException ();
  349. }
  350. #endif
  351. protected override void LoadViewState (object savedState)
  352. {
  353. object first = null;
  354. object second = null;
  355. int[] selected = null;
  356. Triplet triplet = savedState as Triplet;
  357. if (triplet != null) {
  358. first = triplet.First;
  359. second = triplet.Second;
  360. selected = triplet.Third as int[];
  361. }
  362. base.LoadViewState (first);
  363. if (second != null) {
  364. IStateManager manager = Items as IStateManager;
  365. manager.LoadViewState (second);
  366. }
  367. if (selected != null) {
  368. Select (selected);
  369. }
  370. }
  371. protected override void OnDataBinding (EventArgs e)
  372. {
  373. base.OnDataBinding (e);
  374. /* Make sure Items has been initialised */
  375. ListItemCollection listitems = Items;
  376. listitems.Clear ();
  377. IEnumerable list = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
  378. if (list == null) {
  379. return;
  380. }
  381. foreach (object container in list) {
  382. string text = null;
  383. string value = null;
  384. if (DataTextField == String.Empty &&
  385. DataValueField == String.Empty) {
  386. text = container.ToString ();
  387. value = text;
  388. } else {
  389. if (DataTextField != String.Empty) {
  390. text = DataBinder.Eval (container, DataTextField).ToString ();
  391. }
  392. if (DataValueField != String.Empty) {
  393. value = DataBinder.Eval (container, DataValueField).ToString ();
  394. } else {
  395. value = text;
  396. }
  397. if (text == null &&
  398. value != null) {
  399. text = value;
  400. }
  401. }
  402. if (text == null) {
  403. text = String.Empty;
  404. }
  405. if (value == null) {
  406. value = String.Empty;
  407. }
  408. ListItem item = new ListItem (text, value);
  409. listitems.Add (item);
  410. }
  411. }
  412. #if NET_2_0
  413. [MonoTODO]
  414. protected virtual void OnDataPropertyChanged ()
  415. {
  416. RequiresDataBinding = true;
  417. throw new NotImplementedException ();
  418. }
  419. [MonoTODO]
  420. protected virtual void OnDataSourceViewChanged (object sender,
  421. EventArgs e)
  422. {
  423. RequiresDataBinding = true;
  424. throw new NotImplementedException ();
  425. }
  426. [MonoTODO]
  427. protected internal override void OnInit (EventArgs e)
  428. {
  429. /*
  430. if (IsViewStateEnabled == false &&
  431. Page.IsPostBack == true) {
  432. RequiresDataBinding = true;
  433. }
  434. */
  435. throw new NotImplementedException ();
  436. }
  437. [MonoTODO]
  438. protected internal override void OnLoad (EventArgs e)
  439. {
  440. throw new NotImplementedException ();
  441. }
  442. #endif
  443. #if NET_2_0
  444. protected internal
  445. #else
  446. protected
  447. #endif
  448. override void OnPreRender (EventArgs e)
  449. {
  450. base.OnPreRender (e);
  451. if (Page != null) {
  452. Page.RegisterRequiresPostBack (this);
  453. }
  454. }
  455. protected virtual void OnServerChange (EventArgs e)
  456. {
  457. EventHandler handler = (EventHandler)Events[EventServerChange];
  458. if (handler != null) {
  459. handler (this, e);
  460. }
  461. }
  462. protected override void RenderAttributes (HtmlTextWriter w)
  463. {
  464. /* If there is no "name" attribute,
  465. * LoadPostData doesn't work...
  466. */
  467. w.WriteAttribute ("name", Name);
  468. Attributes.Remove ("name");
  469. /* Don't render the databinding attributes */
  470. Attributes.Remove ("datamember");
  471. Attributes.Remove ("datatextfield");
  472. Attributes.Remove ("datavaluefield");
  473. base.RenderAttributes (w);
  474. }
  475. #if NET_2_0
  476. protected internal
  477. #else
  478. protected
  479. #endif
  480. override void RenderChildren (HtmlTextWriter w)
  481. {
  482. base.RenderChildren (w);
  483. if (items == null) {
  484. return;
  485. }
  486. w.WriteLine ();
  487. bool done_sel = false;
  488. int count = items.Count;
  489. for (int i = 0; i < count; i++) {
  490. ListItem item = items[i];
  491. w.Indent++;
  492. /* Write the <option> elements this
  493. * way so that the output HTML matches
  494. * the ms version (can't make
  495. * HtmlTextWriterTag.Option an inline
  496. * element, cos that breaks other
  497. * stuff.)
  498. */
  499. w.WriteBeginTag ("option");
  500. if (item.Selected && !done_sel) {
  501. w.WriteAttribute ("selected", "selected");
  502. if (!Multiple) {
  503. done_sel = true;
  504. }
  505. }
  506. w.WriteAttribute ("value", item.Value);
  507. w.Write (HtmlTextWriter.TagRightChar);
  508. w.Write (item.Text);
  509. w.WriteEndTag ("option");
  510. w.WriteLine ();
  511. w.Indent--;
  512. }
  513. }
  514. protected override object SaveViewState ()
  515. {
  516. object first = null;
  517. object second = null;
  518. object selected = null;
  519. first = base.SaveViewState ();
  520. IStateManager manager = items as IStateManager;
  521. if (manager != null) {
  522. second = manager.SaveViewState ();
  523. }
  524. selected = SelectedIndices;
  525. if (first == null &&
  526. second == null &&
  527. selected == null) {
  528. return (null);
  529. }
  530. return (new Triplet (first, second, selected));
  531. }
  532. /* "internal infrastructure" according to the docs,
  533. * but has some documentation in 2.0
  534. */
  535. protected virtual void Select (int[] selectedIndices)
  536. {
  537. if (items == null) {
  538. return;
  539. }
  540. ClearSelection ();
  541. int count = items.Count;
  542. foreach (int i in selectedIndices) {
  543. if (i >= 0 && i < count) {
  544. items[i].Selected = true;
  545. }
  546. }
  547. }
  548. protected override void TrackViewState ()
  549. {
  550. base.TrackViewState ();
  551. IStateManager manager = items as IStateManager;
  552. if (manager != null) {
  553. manager.TrackViewState ();
  554. }
  555. }
  556. #if NET_2_0
  557. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  558. {
  559. return (LoadPostData (postDataKey, postCollection));
  560. }
  561. protected virtual void RaisePostDataChangedEvent ()
  562. {
  563. RaisePostDataChangedEvent ();
  564. }
  565. #endif
  566. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  567. {
  568. /* postCollection contains the values that are
  569. * selected
  570. */
  571. string[] values = postCollection.GetValues (postDataKey);
  572. bool changed = false;
  573. if (values != null) {
  574. if (Multiple) {
  575. /* We have a set of
  576. * selections. We can't just
  577. * set the new list, because
  578. * we need to know if the set
  579. * has changed from last time
  580. */
  581. int value_len = values.Length;
  582. int[] old_sel = SelectedIndices;
  583. int[] new_sel = new int[value_len];
  584. int old_sel_len = old_sel.Length;
  585. for (int i = 0; i < value_len; i++) {
  586. new_sel[i] = Items.IndexOf (values[i]);
  587. if (old_sel_len != value_len ||
  588. old_sel[i] != new_sel[i]) {
  589. changed = true;
  590. }
  591. }
  592. if (changed) {
  593. Select (new_sel);
  594. }
  595. } else {
  596. /* Just take the first one */
  597. int sel = Items.IndexOf (values[0]);
  598. if (sel != SelectedIndex) {
  599. SelectedIndex = sel;
  600. changed = true;
  601. }
  602. }
  603. }
  604. return (changed);
  605. }
  606. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  607. {
  608. OnServerChange (EventArgs.Empty);
  609. }
  610. }
  611. }