ListControl.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. //
  2. // System.Web.UI.WebControls.ListControl.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.ComponentModel;
  14. using System.ComponentModel.Design;
  15. using System.Web;
  16. using System.Web.UI;
  17. using System.Web.Util;
  18. namespace System.Web.UI.WebControls
  19. {
  20. [DefaultEvent("SelectedIndexChanged")]
  21. #if !NET_2_0
  22. [DefaultProperty("DataSource")]
  23. #endif
  24. [Designer ("System.Web.UI.Design.WebControls.ListControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  25. [DataBindingHandler("System.Web.UI.Design.ListControlDataBindingHandler, " + Consts.AssemblySystem_Design)]
  26. [ParseChildren(true, "Items")]
  27. public abstract class ListControl :
  28. #if NET_2_0
  29. DataBoundControl
  30. #else
  31. WebControl
  32. #endif
  33. {
  34. private static readonly object SelectedIndexChangedEvent = new object();
  35. #if !NET_2_0
  36. private object dataSource;
  37. #endif
  38. private ListItemCollection items;
  39. private int cachedSelectedIndex = -1;
  40. private string cachedSelectedValue;
  41. #if !NET_2_0
  42. public ListControl(): base(HtmlTextWriterTag.Select)
  43. {
  44. }
  45. #else
  46. protected override HtmlTextWriterTag TagKey {
  47. get { return HtmlTextWriterTag.Select; }
  48. }
  49. #endif
  50. [WebCategory ("Action")]
  51. [WebSysDescription ("Raised when the selected index entry has changed.")]
  52. public event EventHandler SelectedIndexChanged
  53. {
  54. add
  55. {
  56. Events.AddHandler(SelectedIndexChangedEvent, value);
  57. }
  58. remove
  59. {
  60. Events.RemoveHandler(SelectedIndexChangedEvent, value);
  61. }
  62. }
  63. [DefaultValue (false), WebCategory ("Behavior")]
  64. [WebSysDescription ("The control automatically posts back after changing the text.")]
  65. public virtual bool AutoPostBack
  66. {
  67. get
  68. {
  69. object o = ViewState["AutoPostBack"];
  70. if(o!=null)
  71. return (bool)o;
  72. return false;
  73. }
  74. set
  75. {
  76. ViewState["AutoPostBack"] = value;
  77. }
  78. }
  79. #if !NET_2_0
  80. [DefaultValue (""), WebCategory ("Data")]
  81. [WebSysDescription ("The name of the table that is used for binding when a DataSource is specified.")]
  82. public virtual string DataMember
  83. {
  84. get
  85. {
  86. object o = ViewState["DataMember"];
  87. if(o!=null)
  88. return (string)o;
  89. return String.Empty;
  90. }
  91. set
  92. {
  93. ViewState["DataMember"] = value;
  94. }
  95. }
  96. [DefaultValue (null), Bindable (true), WebCategory ("Data")]
  97. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  98. [WebSysDescription ("The DataSource that is used for data-binding.")]
  99. public virtual object DataSource
  100. {
  101. get
  102. {
  103. return dataSource;
  104. }
  105. set
  106. {
  107. if(value == null || value is IListSource || value is IEnumerable) {
  108. dataSource = value;
  109. return;
  110. }
  111. throw new ArgumentException(HttpRuntime.FormatResourceString(ID, "Invalid DataSource Type"));
  112. }
  113. }
  114. #endif
  115. [DefaultValue (""), WebCategory ("Data")]
  116. [WebSysDescription ("The field in the datatable that provides the text entry.")]
  117. public virtual string DataTextField
  118. {
  119. get
  120. {
  121. object o = ViewState["DataTextField"];
  122. if(o!=null)
  123. return (string)o;
  124. return String.Empty;
  125. }
  126. set
  127. {
  128. ViewState["DataTextField"] = value;
  129. }
  130. }
  131. [DefaultValue (""), WebCategory ("Data")]
  132. [WebSysDescription ("Specifies a formatting rule for the texts that are returned.")]
  133. public virtual string DataTextFormatString
  134. {
  135. get
  136. {
  137. object o = ViewState["DataTextFormatString"];
  138. if(o!=null)
  139. return (string)o;
  140. return String.Empty;
  141. }
  142. set
  143. {
  144. ViewState["DataTextFormatString"] = value;
  145. }
  146. }
  147. [DefaultValue (""), WebCategory ("Data")]
  148. [WebSysDescription ("The field in the datatable that provides the entry value.")]
  149. public virtual string DataValueField
  150. {
  151. get
  152. {
  153. object o = ViewState["DataValueField"];
  154. if(o!=null)
  155. return (string)o;
  156. return String.Empty;
  157. }
  158. set
  159. {
  160. ViewState["DataValueField"] = value;
  161. }
  162. }
  163. [DefaultValue (null), MergableProperty (false), WebCategory ("Misc")]
  164. [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
  165. [WebSysDescription ("A collection of all items contained in this list.")]
  166. public virtual ListItemCollection Items
  167. {
  168. get
  169. {
  170. if(items==null)
  171. {
  172. items = new ListItemCollection();
  173. if(IsTrackingViewState)
  174. {
  175. items.TrackViewState();
  176. }
  177. }
  178. return items;
  179. }
  180. }
  181. [DefaultValue (0), Bindable (true), WebCategory ("Misc")]
  182. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  183. [WebSysDescription ("The index number of the currently selected ListItem.")]
  184. public virtual int SelectedIndex
  185. {
  186. get {
  187. ListItemCollection items = Items;
  188. int last = items.Count;
  189. for (int i = 0; i < last; i++) {
  190. if (items [i].Selected)
  191. return i;
  192. }
  193. return -1;
  194. }
  195. set {
  196. if (Items.Count == 0)
  197. {
  198. cachedSelectedIndex = value;
  199. return;
  200. }
  201. if ((value < -1) || (value >= Items.Count))
  202. throw new ArgumentOutOfRangeException ();
  203. ClearSelection ();
  204. if (value != -1)
  205. Items [value].Selected = true;
  206. }
  207. }
  208. [DefaultValue (null), WebCategory ("Misc")]
  209. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  210. [WebSysDescription ("The currently selected ListItem.")]
  211. public virtual ListItem SelectedItem
  212. {
  213. get
  214. {
  215. int idx = SelectedIndex;
  216. if (idx < 0)
  217. return null;
  218. return Items [idx];
  219. }
  220. }
  221. #if NET_1_1
  222. [DefaultValue (""), Bindable (true), WebCategory ("Misc")]
  223. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  224. [WebSysDescription ("The value of the currently selected ListItem.")]
  225. public virtual string SelectedValue {
  226. get {
  227. int idx = SelectedIndex;
  228. if (idx == -1)
  229. return "";
  230. return Items [idx].Value;
  231. }
  232. set {
  233. ListItem item = null;
  234. if (value != null) {
  235. if (Items.Count > 0) {
  236. item = Items.FindByValue (value);
  237. if (item == null)
  238. throw new ArgumentOutOfRangeException ("value");
  239. } else {
  240. cachedSelectedValue = value;
  241. return;
  242. }
  243. }
  244. ClearSelection ();
  245. if (item != null)
  246. item.Selected = true;
  247. }
  248. }
  249. #endif
  250. internal virtual ArrayList SelectedIndices
  251. {
  252. get
  253. {
  254. ArrayList si = new ArrayList();
  255. for(int i=0; i < Items.Count; i++)
  256. {
  257. if(Items[i].Selected)
  258. si.Add(i);
  259. }
  260. return si;
  261. }
  262. }
  263. internal void Select(ArrayList indices)
  264. {
  265. ClearSelection();
  266. foreach(object intObj in indices)
  267. {
  268. int index = (int)intObj;
  269. if(index >= 0 && index < Items.Count)
  270. Items[index].Selected = true;
  271. }
  272. }
  273. public virtual void ClearSelection()
  274. {
  275. for(int i=0; i < Items.Count; i++)
  276. {
  277. Items[i].Selected = false;
  278. }
  279. }
  280. protected override void LoadViewState(object savedState)
  281. {
  282. //Order: BaseClass, Items (Collection), Indices
  283. if(savedState != null && savedState is Triplet)
  284. {
  285. Triplet state = (Triplet)savedState;
  286. base.LoadViewState(state.First);
  287. Items.LoadViewState(state.Second);
  288. object indices = state.Third;
  289. if(indices != null)
  290. {
  291. Select((ArrayList)indices);
  292. }
  293. }
  294. }
  295. #if NET_2_0
  296. protected override void PerformDataBinding ()
  297. {
  298. base.PerformDataBinding ();
  299. IEnumerable ds = GetResolvedDataSource ();
  300. #else
  301. protected override void OnDataBinding(EventArgs e)
  302. {
  303. base.OnDataBinding(e);
  304. IEnumerable ds = DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
  305. #endif
  306. if(ds != null) {
  307. string dtf = DataTextField;
  308. string dvf = DataValueField;
  309. string dtfs = DataTextFormatString;
  310. if (dtfs.Length == 0)
  311. dtfs = "{0}";
  312. Items.Clear();
  313. bool dontUseProperties = (dtf.Length == 0 && dvf.Length == 0);
  314. foreach (object current in ds) {
  315. ListItem li = new ListItem();
  316. if (dontUseProperties){
  317. li.Text = String.Format (dtfs, current);
  318. li.Value = current.ToString ();
  319. Items.Add (li);
  320. continue;
  321. }
  322. object o;
  323. if (dtf.Length > 0) {
  324. o = DataBinder.GetPropertyValue (current, dtf, dtfs);
  325. li.Text = o.ToString ();
  326. }
  327. if (dvf.Length > 0) {
  328. o = DataBinder.GetPropertyValue (current, dvf, null);
  329. li.Value = o.ToString ();
  330. }
  331. Items.Add(li);
  332. }
  333. }
  334. if (cachedSelectedValue != null) {
  335. int index = Items.FindByValueInternal (cachedSelectedValue);
  336. if (index == -1)
  337. throw new ArgumentOutOfRangeException("value");
  338. if (cachedSelectedIndex != -1 && cachedSelectedIndex != index)
  339. throw new ArgumentException(HttpRuntime.FormatResourceString(
  340. "Attributes_mutually_exclusive", "Selected Index", "Selected Value"));
  341. SelectedIndex = index;
  342. cachedSelectedIndex = -1;
  343. cachedSelectedValue = null;
  344. return;
  345. }
  346. if (cachedSelectedIndex != -1) {
  347. SelectedIndex = cachedSelectedIndex;
  348. cachedSelectedIndex = -1;
  349. }
  350. }
  351. protected virtual void OnSelectedIndexChanged(EventArgs e)
  352. {
  353. if(Events!=null)
  354. {
  355. EventHandler eh = (EventHandler)(Events[SelectedIndexChangedEvent]);
  356. if(eh!=null)
  357. eh(this, e);
  358. }
  359. }
  360. protected override void OnPreRender (EventArgs e)
  361. {
  362. base.OnPreRender(e);
  363. }
  364. protected override object SaveViewState()
  365. {
  366. //Order: BaseClass, Items (Collection), Indices
  367. object vs = base.SaveViewState();
  368. object itemSvs = Items.SaveViewState();
  369. object indices = null;
  370. if (SaveSelectedIndicesViewState)
  371. indices = SelectedIndices;
  372. if (vs != null || itemSvs != null || indices != null)
  373. return new Triplet(vs, itemSvs, indices);
  374. return null;
  375. }
  376. protected override void TrackViewState()
  377. {
  378. base.TrackViewState();
  379. Items.TrackViewState();
  380. }
  381. private bool SaveSelectedIndicesViewState {
  382. get {
  383. if (Events[SelectedIndexChangedEvent] == null && Enabled && Visible) {
  384. Type t = GetType();
  385. // If I am a derivative, let it take of storing the selected indices.
  386. if (t == typeof(DropDownList) || t == typeof(ListBox) ||
  387. t == typeof(CheckBoxList) || t == typeof(RadioButtonList))
  388. return false;
  389. }
  390. return true;
  391. }
  392. }
  393. }
  394. }