ListControl.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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_1_2
  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_1_2
  29. DataBoundControl
  30. #else
  31. WebControl
  32. #endif
  33. {
  34. private static readonly object SelectedIndexChangedEvent = new object();
  35. #if !NET_1_2
  36. private object dataSource;
  37. #endif
  38. private ListItemCollection items;
  39. private int cachedSelectedIndex = -1;
  40. private string cachedSelectedValue;
  41. #if !NET_1_2
  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_1_2
  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 (value < -1 || value > Items.Count)
  197. throw new ArgumentOutOfRangeException ();
  198. ClearSelection ();
  199. if (value != -1)
  200. Items [value].Selected = true;
  201. }
  202. }
  203. [DefaultValue (null), WebCategory ("Misc")]
  204. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  205. [WebSysDescription ("The currently selected ListItem.")]
  206. public virtual ListItem SelectedItem
  207. {
  208. get
  209. {
  210. int idx = SelectedIndex;
  211. if (idx < 0)
  212. return null;
  213. return Items [idx];
  214. }
  215. }
  216. #if NET_1_1
  217. [DefaultValue (""), Bindable (true), WebCategory ("Misc")]
  218. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  219. [WebSysDescription ("The value of the currently selected ListItem.")]
  220. public virtual string SelectedValue {
  221. get {
  222. int idx = SelectedIndex;
  223. if (idx == -1)
  224. return "";
  225. return Items [idx].Value;
  226. }
  227. set {
  228. ListItem item = null;
  229. if (value != null) {
  230. if (Items.Count > 0) {
  231. item = Items.FindByValue (value);
  232. if (item == null)
  233. throw new ArgumentOutOfRangeException ("value");
  234. } else {
  235. cachedSelectedValue = value;
  236. return;
  237. }
  238. }
  239. ClearSelection ();
  240. if (item != null)
  241. item.Selected = true;
  242. }
  243. }
  244. #endif
  245. internal virtual ArrayList SelectedIndices
  246. {
  247. get
  248. {
  249. ArrayList si = new ArrayList();
  250. for(int i=0; i < Items.Count; i++)
  251. {
  252. if(Items[i].Selected)
  253. si.Add(i);
  254. }
  255. return si;
  256. }
  257. }
  258. internal void Select(ArrayList indices)
  259. {
  260. ClearSelection();
  261. foreach(object intObj in indices)
  262. {
  263. int index = (int)intObj;
  264. if(index >= 0 && index < Items.Count)
  265. Items[index].Selected = true;
  266. }
  267. }
  268. public virtual void ClearSelection()
  269. {
  270. for(int i=0; i < Items.Count; i++)
  271. {
  272. Items[i].Selected = false;
  273. }
  274. }
  275. protected override void LoadViewState(object savedState)
  276. {
  277. //Order: BaseClass, Items (Collection), Indices
  278. if(savedState != null && savedState is Triplet)
  279. {
  280. Triplet state = (Triplet)savedState;
  281. base.LoadViewState(state.First);
  282. Items.LoadViewState(state.Second);
  283. object indices = state.Third;
  284. if(indices != null)
  285. {
  286. Select((ArrayList)indices);
  287. }
  288. }
  289. }
  290. #if NET_1_2
  291. protected override void PerformDataBinding ()
  292. {
  293. base.PerformDataBinding ();
  294. IEnumerable ds = GetResolvedDataSource ();
  295. #else
  296. protected override void OnDataBinding(EventArgs e)
  297. {
  298. base.OnDataBinding(e);
  299. IEnumerable ds = DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
  300. #endif
  301. if(ds != null) {
  302. string dtf = DataTextField;
  303. string dvf = DataValueField;
  304. string dtfs = DataTextFormatString;
  305. if (dtfs.Length == 0)
  306. dtfs = "{0}";
  307. Items.Clear();
  308. bool dontUseProperties = (dtf.Length == 0 && dvf.Length == 0);
  309. foreach (object current in ds) {
  310. ListItem li = new ListItem();
  311. if (dontUseProperties){
  312. li.Text = String.Format (dtfs, current);
  313. li.Value = current.ToString ();
  314. Items.Add (li);
  315. continue;
  316. }
  317. object o;
  318. if (dtf.Length > 0) {
  319. o = DataBinder.GetPropertyValue (current, dtf, dtfs);
  320. li.Text = o.ToString ();
  321. }
  322. if (dvf.Length > 0) {
  323. o = DataBinder.GetPropertyValue (current, dvf, null);
  324. li.Value = o.ToString ();
  325. }
  326. Items.Add(li);
  327. }
  328. }
  329. if (cachedSelectedValue != null) {
  330. int index = Items.FindByValueInternal (cachedSelectedValue);
  331. if (index == -1)
  332. throw new ArgumentOutOfRangeException("value");
  333. if (cachedSelectedIndex != -1 && cachedSelectedIndex != index)
  334. throw new ArgumentException(HttpRuntime.FormatResourceString(
  335. "Attributes_mutually_exclusive", "Selected Index", "Selected Value"));
  336. SelectedIndex = index;
  337. cachedSelectedIndex = -1;
  338. cachedSelectedValue = null;
  339. return;
  340. }
  341. if (cachedSelectedIndex != -1) {
  342. SelectedIndex = cachedSelectedIndex;
  343. cachedSelectedIndex = -1;
  344. }
  345. }
  346. protected virtual void OnSelectedIndexChanged(EventArgs e)
  347. {
  348. if(Events!=null)
  349. {
  350. EventHandler eh = (EventHandler)(Events[SelectedIndexChangedEvent]);
  351. if(eh!=null)
  352. eh(this, e);
  353. }
  354. }
  355. protected override void OnPreRender (EventArgs e)
  356. {
  357. base.OnPreRender(e);
  358. }
  359. protected override object SaveViewState()
  360. {
  361. //Order: BaseClass, Items (Collection), Indices
  362. object vs = base.SaveViewState();
  363. object itemSvs = Items.SaveViewState();
  364. object indices = null;
  365. if (SaveSelectedIndicesViewState)
  366. indices = SelectedIndices;
  367. if (vs != null || itemSvs != null || indices != null)
  368. return new Triplet(vs, itemSvs, indices);
  369. return null;
  370. }
  371. protected override void TrackViewState()
  372. {
  373. base.TrackViewState();
  374. Items.TrackViewState();
  375. }
  376. private bool SaveSelectedIndicesViewState {
  377. get {
  378. if (Events[SelectedIndexChangedEvent] == null && Enabled && Visible) {
  379. Type t = GetType();
  380. // If I am a derivative, let it take of storing the selected indices.
  381. if (t == typeof(DropDownList) || t == typeof(ListBox) ||
  382. t == typeof(CheckBoxList) || t == typeof(RadioButtonList))
  383. return false;
  384. }
  385. return true;
  386. }
  387. }
  388. }
  389. }