BaseDataList.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. //
  2. // System.Web.UI.WebControls.BaseDataList.cs
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[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.Collections;
  29. using System.ComponentModel;
  30. using System.Security.Permissions;
  31. namespace System.Web.UI.WebControls {
  32. // CAS
  33. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. // attributes
  36. [DefaultEvent ("SelectedIndexChanged")]
  37. [DefaultProperty ("DataSource")]
  38. [Designer ("System.Web.UI.Design.WebControls.BaseDataListDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  39. public abstract class BaseDataList : WebControl {
  40. private static readonly object selectedIndexChangedEvent = new object ();
  41. private DataKeyCollection keycoll;
  42. private object source;
  43. #if NET_2_0
  44. //private string dataSourceId;
  45. IDataSource boundDataSource = null;
  46. private bool initialized;
  47. private bool requiresDataBinding;
  48. private DataSourceSelectArguments selectArguments;
  49. private IEnumerable data;
  50. #endif
  51. protected BaseDataList ()
  52. {
  53. }
  54. [DefaultValue ("")]
  55. #if NET_2_0
  56. [Localizable (true)]
  57. #endif
  58. [WebSysDescription ("")]
  59. [WebCategory ("Accessibility")]
  60. public virtual string Caption {
  61. get { return ViewState.GetString ("Caption", ""); }
  62. set {
  63. if (value == null)
  64. ViewState.Remove ("Caption");
  65. else
  66. ViewState ["Caption"] = value;
  67. }
  68. }
  69. [DefaultValue (TableCaptionAlign.NotSet)]
  70. public virtual TableCaptionAlign CaptionAlign {
  71. get { return (TableCaptionAlign) ViewState.GetInt ("CaptionAlign", (int)TableCaptionAlign.NotSet); }
  72. set {
  73. if ((value < TableCaptionAlign.NotSet) || (value > TableCaptionAlign.Right))
  74. throw new ArgumentOutOfRangeException (Locale.GetText ("Invalid TableCaptionAlign value."));
  75. ViewState ["CaptionAlign"] = value;
  76. }
  77. }
  78. #if ONLY_1_1
  79. [Bindable (true)]
  80. #endif
  81. [DefaultValue (-1)]
  82. [WebSysDescription("")]
  83. [WebCategory("Layout")]
  84. public virtual int CellPadding {
  85. get {
  86. if (!ControlStyleCreated)
  87. return -1; // default value
  88. return TableStyle.CellPadding;
  89. }
  90. set { TableStyle.CellPadding = value; }
  91. }
  92. #if ONLY_1_1
  93. [Bindable (true)]
  94. #endif
  95. [DefaultValue (0)]
  96. [WebSysDescription("")]
  97. [WebCategory("Layout")]
  98. public virtual int CellSpacing {
  99. get {
  100. if (!ControlStyleCreated)
  101. return 0; // default value
  102. return TableStyle.CellSpacing;
  103. }
  104. set { TableStyle.CellSpacing = value; }
  105. }
  106. public override ControlCollection Controls {
  107. get {
  108. EnsureChildControls ();
  109. return base.Controls;
  110. }
  111. }
  112. [DefaultValue ("")]
  113. #if NET_2_0
  114. [Themeable (false)]
  115. #endif
  116. [MonoTODO ("incomplete")]
  117. [WebSysDescription("")]
  118. [WebCategory("Data")]
  119. public virtual string DataKeyField {
  120. get { return ViewState.GetString ("DataKeyField", ""); }
  121. set {
  122. if (value == null)
  123. ViewState.Remove ("DataKeyField");
  124. else
  125. ViewState ["DataKeyField"] = value;
  126. }
  127. }
  128. [Browsable (false)]
  129. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  130. [WebSysDescription("")]
  131. [WebCategory("Data")]
  132. public DataKeyCollection DataKeys {
  133. get {
  134. if (keycoll == null)
  135. keycoll = new DataKeyCollection (DataKeysArray);
  136. return keycoll;
  137. }
  138. }
  139. protected ArrayList DataKeysArray {
  140. get {
  141. ArrayList keys = (ArrayList) ViewState ["DataKeys"];
  142. if (keys == null) {
  143. keys = new ArrayList ();
  144. ViewState ["DataKeys"] = keys;
  145. }
  146. return keys;
  147. }
  148. }
  149. [DefaultValue ("")]
  150. #if NET_2_0
  151. [Themeable (false)]
  152. #endif
  153. [WebSysDescription("")]
  154. [WebCategory("Data")]
  155. public string DataMember {
  156. get { return ViewState.GetString ("DataMember", ""); }
  157. set {
  158. if (value == null)
  159. ViewState.Remove ("DataMember");
  160. else
  161. ViewState ["DataMember"] = value;
  162. #if NET_2_0
  163. if (!Initialized)
  164. OnDataPropertyChanged ();
  165. #endif
  166. }
  167. }
  168. [Bindable (true)]
  169. [DefaultValue (null)]
  170. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  171. #if NET_2_0
  172. [Themeable (false)]
  173. #endif
  174. [WebSysDescription("")]
  175. [WebCategory("Data")]
  176. public virtual object DataSource {
  177. get { return source; }
  178. set {
  179. if ((value == null) || (value is IEnumerable) || (value is IListSource)) {
  180. #if NET_2_0
  181. // FIXME - can't duplicate in a test case ? LAMESPEC ?
  182. // can't duplicate in a test case
  183. // if ((dataSourceId != null) && (dataSourceId.Length != 0))
  184. // throw new HttpException (Locale.GetText ("DataSourceID is already set."));
  185. source = value;
  186. if (!Initialized)
  187. OnDataPropertyChanged ();
  188. #else
  189. source = value;
  190. #endif
  191. } else {
  192. string msg = Locale.GetText ("Invalid data source. This requires an object implementing {0} or {1}.",
  193. "IEnumerable", "IListSource");
  194. throw new ArgumentException (msg);
  195. }
  196. }
  197. }
  198. #if ONLY_1_1
  199. [Bindable (true)]
  200. #endif
  201. [DefaultValue (GridLines.Both)]
  202. [WebSysDescription("")]
  203. [WebCategory("Appearance")]
  204. public virtual GridLines GridLines {
  205. get {
  206. if (!ControlStyleCreated)
  207. return GridLines.Both; // default value
  208. return TableStyle.GridLines;
  209. }
  210. set { TableStyle.GridLines = value; }
  211. }
  212. #if ONLY_1_1
  213. [Bindable (true)]
  214. #endif
  215. [Category ("Layout")]
  216. [DefaultValue (HorizontalAlign.NotSet)]
  217. [WebSysDescription("")]
  218. public virtual HorizontalAlign HorizontalAlign {
  219. get {
  220. if (!ControlStyleCreated)
  221. return HorizontalAlign.NotSet; // default value
  222. return TableStyle.HorizontalAlign;
  223. }
  224. set { TableStyle.HorizontalAlign = value; }
  225. }
  226. [DefaultValue (false)]
  227. public virtual bool UseAccessibleHeader {
  228. get { return ViewState.GetBool ("UseAccessibleHeader", false); }
  229. set { ViewState ["UseAccessibleHeader"] = value; }
  230. }
  231. #if NET_2_0
  232. [DefaultValue ("")]
  233. [IDReferenceProperty (typeof (DataSourceControl))]
  234. [Themeable (false)]
  235. public virtual string DataSourceID {
  236. get { return ViewState.GetString ("DataSourceID", ""); }
  237. set {
  238. // LAMESPEC ? this is documented as an HttpException in beta2
  239. if (source != null)
  240. throw new InvalidOperationException (Locale.GetText ("DataSource is already set."));
  241. ViewState ["DataSourceID"] = value;
  242. if (!Initialized)
  243. OnDataPropertyChanged ();
  244. }
  245. }
  246. protected bool Initialized {
  247. get { return initialized; }
  248. }
  249. // as documented in BaseDataBoundControl
  250. protected bool IsBoundUsingDataSourceID {
  251. get { return (DataSourceID.Length != 0); }
  252. }
  253. // doc says ?automatically? called by ASP.NET
  254. protected bool RequiresDataBinding {
  255. get { return requiresDataBinding; }
  256. set { requiresDataBinding = value; }
  257. }
  258. protected DataSourceSelectArguments SelectArguments {
  259. get {
  260. if (selectArguments == null)
  261. selectArguments = CreateDataSourceSelectArguments ();
  262. return selectArguments;
  263. }
  264. }
  265. #endif
  266. private TableStyle TableStyle {
  267. // this will throw an InvalidCasException just like we need
  268. get { return (TableStyle) ControlStyle; }
  269. }
  270. protected override void AddParsedSubObject (object obj)
  271. {
  272. // don't accept controls
  273. }
  274. // see Kothari, page 435
  275. #if NET_2_0
  276. protected internal
  277. #else
  278. protected
  279. #endif
  280. override void CreateChildControls ()
  281. {
  282. // We are recreating the children from viewstate
  283. if (HasControls ())
  284. Controls.Clear();
  285. // If presents, build the children from the viewstate
  286. if (ViewState ["Items"] != null)
  287. CreateControlHierarchy (false);
  288. }
  289. protected abstract void CreateControlHierarchy (bool useDataSource);
  290. // see Kothari, page 434
  291. // see also: Control.DataBind on Fx 2.0 beta2 documentation
  292. public override void DataBind ()
  293. {
  294. // unlike most samples we don't call base.OnDataBinding
  295. // because we override it in this class
  296. OnDataBinding (EventArgs.Empty);
  297. // Clear, if required, then recreate the control hierarchy
  298. if (HasControls ())
  299. Controls.Clear ();
  300. if (HasChildViewState)
  301. ClearChildViewState ();
  302. if (!IsTrackingViewState)
  303. TrackViewState ();
  304. CreateControlHierarchy (true);
  305. // Indicate that child controls have been created, preventing
  306. // CreateChildControls from getting called.
  307. ChildControlsCreated = true;
  308. }
  309. #if NET_2_0
  310. protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
  311. {
  312. return DataSourceSelectArguments.Empty;
  313. }
  314. // best documentation is (again) in BaseDataBoundControl
  315. protected void EnsureDataBound ()
  316. {
  317. if (IsBoundUsingDataSourceID && RequiresDataBinding)
  318. DataBind ();
  319. }
  320. private void SelectCallback (IEnumerable data)
  321. {
  322. this.data = data;
  323. }
  324. protected virtual IEnumerable GetData ()
  325. {
  326. if (DataSourceID.Length == 0)
  327. return null;
  328. if (boundDataSource == null)
  329. ConnectToDataSource ();
  330. DataSourceView dsv = boundDataSource.GetView (String.Empty);
  331. dsv.Select (SelectArguments, new DataSourceViewSelectCallback (SelectCallback));
  332. return data;
  333. }
  334. #endif
  335. protected override void OnDataBinding (EventArgs e)
  336. {
  337. base.OnDataBinding (e);
  338. }
  339. #if NET_2_0
  340. protected virtual void OnDataPropertyChanged ()
  341. {
  342. }
  343. protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
  344. {
  345. RequiresDataBinding = true;
  346. }
  347. protected internal override void OnInit (EventArgs e)
  348. {
  349. base.OnInit (e);
  350. }
  351. protected internal override void OnLoad (EventArgs e)
  352. {
  353. if ((Page != null) && !Page.IsPostBack)
  354. RequiresDataBinding = true;
  355. if (IsBoundUsingDataSourceID)
  356. ConnectToDataSource ();
  357. initialized = true;
  358. base.OnLoad (e);
  359. }
  360. protected internal override void OnPreRender (EventArgs e)
  361. {
  362. EnsureDataBound ();
  363. base.OnPreRender (e);
  364. }
  365. #endif
  366. protected virtual void OnSelectedIndexChanged (EventArgs e)
  367. {
  368. EventHandler selectedIndexChanged = (EventHandler) Events [selectedIndexChangedEvent];
  369. if (selectedIndexChanged != null)
  370. selectedIndexChanged (this, e);
  371. }
  372. protected abstract void PrepareControlHierarchy ();
  373. #if NET_2_0
  374. protected internal
  375. #else
  376. protected
  377. #endif
  378. override void Render (HtmlTextWriter writer)
  379. {
  380. PrepareControlHierarchy ();
  381. // don't call base class or RenderBegin|EndTag
  382. // or we'll get an extra <span></span>
  383. RenderContents (writer);
  384. }
  385. [WebSysDescription("")]
  386. [WebCategory("Action")]
  387. public event EventHandler SelectedIndexChanged {
  388. add { Events.AddHandler (selectedIndexChangedEvent, value); }
  389. remove { Events.RemoveHandler (selectedIndexChangedEvent, value); }
  390. }
  391. static public bool IsBindableType (Type type)
  392. {
  393. // I can't believe how many NRE are possible in System.Web
  394. if (type == null) // Type.GetTypeCode no longer throws when a null is passed.
  395. throw new NullReferenceException ();
  396. switch (Type.GetTypeCode (type)) {
  397. case TypeCode.Boolean:
  398. case TypeCode.Byte:
  399. case TypeCode.SByte:
  400. case TypeCode.Int16:
  401. case TypeCode.UInt16:
  402. case TypeCode.Int32:
  403. case TypeCode.UInt32:
  404. case TypeCode.Int64:
  405. case TypeCode.UInt64:
  406. case TypeCode.Char:
  407. case TypeCode.Double:
  408. case TypeCode.Single:
  409. case TypeCode.DateTime:
  410. case TypeCode.Decimal:
  411. case TypeCode.String:
  412. return true;
  413. default:
  414. return false;
  415. }
  416. }
  417. #if NET_2_0
  418. void ConnectToDataSource ()
  419. {
  420. if (NamingContainer != null)
  421. boundDataSource = (NamingContainer.FindControl (DataSourceID) as IDataSource);
  422. if (boundDataSource == null) {
  423. if (Parent != null)
  424. boundDataSource = (Parent.FindControl (DataSourceID) as IDataSource);
  425. if (boundDataSource == null)
  426. throw new HttpException (Locale.GetText ("Coulnd't find a DataSource named '{0}'.", DataSourceID));
  427. }
  428. DataSourceView dsv = boundDataSource.GetView (String.Empty);
  429. dsv.DataSourceViewChanged += new EventHandler (OnDataSourceViewChanged);
  430. }
  431. #endif
  432. }
  433. }