BaseDataList.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. OnDataPropertyChanged ();
  164. #endif
  165. }
  166. }
  167. [Bindable (true)]
  168. [DefaultValue (null)]
  169. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  170. #if NET_2_0
  171. [Themeable (false)]
  172. #endif
  173. [WebSysDescription("")]
  174. [WebCategory("Data")]
  175. public virtual object DataSource {
  176. get { return source; }
  177. set {
  178. if ((value == null) || (value is IEnumerable) || (value is IListSource)) {
  179. #if NET_2_0
  180. // FIXME - can't duplicate in a test case ? LAMESPEC ?
  181. // can't duplicate in a test case
  182. // if ((dataSourceId != null) && (dataSourceId.Length != 0))
  183. // throw new HttpException (Locale.GetText ("DataSourceID is already set."));
  184. source = value;
  185. OnDataPropertyChanged ();
  186. #else
  187. source = value;
  188. #endif
  189. } else {
  190. string msg = Locale.GetText ("Invalid data source. This requires an object implementing {0} or {1}.",
  191. "IEnumerable", "IListSource");
  192. throw new ArgumentException (msg);
  193. }
  194. }
  195. }
  196. #if ONLY_1_1
  197. [Bindable (true)]
  198. #endif
  199. [DefaultValue (GridLines.Both)]
  200. [WebSysDescription("")]
  201. [WebCategory("Appearance")]
  202. public virtual GridLines GridLines {
  203. get {
  204. if (!ControlStyleCreated)
  205. return GridLines.Both; // default value
  206. return TableStyle.GridLines;
  207. }
  208. set { TableStyle.GridLines = value; }
  209. }
  210. #if ONLY_1_1
  211. [Bindable (true)]
  212. #endif
  213. [Category ("Layout")]
  214. [DefaultValue (HorizontalAlign.NotSet)]
  215. [WebSysDescription("")]
  216. public virtual HorizontalAlign HorizontalAlign {
  217. get {
  218. if (!ControlStyleCreated)
  219. return HorizontalAlign.NotSet; // default value
  220. return TableStyle.HorizontalAlign;
  221. }
  222. set { TableStyle.HorizontalAlign = value; }
  223. }
  224. [DefaultValue (false)]
  225. public virtual bool UseAccessibleHeader {
  226. get { return ViewState.GetBool ("UseAccessibleHeader", false); }
  227. set { ViewState ["UseAccessibleHeader"] = value; }
  228. }
  229. #if NET_2_0
  230. [DefaultValue ("")]
  231. [IDReferenceProperty (typeof (DataSourceControl))]
  232. [Themeable (false)]
  233. public virtual string DataSourceID {
  234. get { return ViewState.GetString ("DataSourceID", ""); }
  235. set {
  236. // LAMESPEC ? this is documented as an HttpException in beta2
  237. if (source != null)
  238. throw new InvalidOperationException (Locale.GetText ("DataSource is already set."));
  239. ViewState ["DataSourceID"] = value;
  240. OnDataPropertyChanged ();
  241. }
  242. }
  243. protected bool Initialized {
  244. get { return initialized; }
  245. }
  246. // as documented in BaseDataBoundControl
  247. protected bool IsBoundUsingDataSourceID {
  248. get { return (DataSourceID.Length != 0); }
  249. }
  250. // doc says ?automatically? called by ASP.NET
  251. protected bool RequiresDataBinding {
  252. get { return requiresDataBinding; }
  253. set { requiresDataBinding = value; }
  254. }
  255. protected DataSourceSelectArguments SelectArguments {
  256. get {
  257. if (selectArguments == null)
  258. selectArguments = CreateDataSourceSelectArguments ();
  259. return selectArguments;
  260. }
  261. }
  262. #endif
  263. private TableStyle TableStyle {
  264. // this will throw an InvalidCasException just like we need
  265. get { return (TableStyle) ControlStyle; }
  266. }
  267. protected override void AddParsedSubObject (object obj)
  268. {
  269. // don't accept controls
  270. }
  271. // see Kothari, page 435
  272. #if NET_2_0
  273. protected internal
  274. #else
  275. protected
  276. #endif
  277. override void CreateChildControls ()
  278. {
  279. // We are recreating the children from viewstate
  280. if (HasControls ())
  281. base.Controls.Clear();
  282. #if NET_2_0
  283. if (IsDataBound)
  284. CreateControlHierarchy (false);
  285. else if (RequiresDataBinding)
  286. EnsureDataBound ();
  287. #else
  288. // If presents, build the children from the viewstate
  289. if (ViewState ["Items"] != null)
  290. CreateControlHierarchy (false);
  291. #endif
  292. }
  293. protected abstract void CreateControlHierarchy (bool useDataSource);
  294. // see Kothari, page 434
  295. // see also: Control.DataBind on Fx 2.0 beta2 documentation
  296. public override void DataBind ()
  297. {
  298. // unlike most samples we don't call base.OnDataBinding
  299. // because we override it in this class
  300. OnDataBinding (EventArgs.Empty);
  301. // Clear, if required, then recreate the control hierarchy
  302. if (HasControls ())
  303. Controls.Clear ();
  304. if (HasChildViewState)
  305. ClearChildViewState ();
  306. if (!IsTrackingViewState)
  307. TrackViewState ();
  308. CreateControlHierarchy (true);
  309. // Indicate that child controls have been created, preventing
  310. // CreateChildControls from getting called.
  311. ChildControlsCreated = true;
  312. #if NET_2_0
  313. RequiresDataBinding = false;
  314. IsDataBound = true;
  315. #endif
  316. }
  317. #if NET_2_0
  318. protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
  319. {
  320. return DataSourceSelectArguments.Empty;
  321. }
  322. // best documentation is (again) in BaseDataBoundControl
  323. protected void EnsureDataBound ()
  324. {
  325. if (IsBoundUsingDataSourceID && RequiresDataBinding)
  326. DataBind ();
  327. }
  328. private void SelectCallback (IEnumerable data)
  329. {
  330. this.data = data;
  331. }
  332. protected virtual IEnumerable GetData ()
  333. {
  334. if (DataSourceID.Length == 0)
  335. return null;
  336. if (boundDataSource == null)
  337. ConnectToDataSource ();
  338. DataSourceView dsv = boundDataSource.GetView (String.Empty);
  339. dsv.Select (SelectArguments, new DataSourceViewSelectCallback (SelectCallback));
  340. return data;
  341. }
  342. bool IsDataBound {
  343. get {
  344. return ViewState.GetBool ("_DataBound", false);
  345. }
  346. set {
  347. ViewState ["_DataBound"] = value;
  348. }
  349. }
  350. #endif
  351. protected override void OnDataBinding (EventArgs e)
  352. {
  353. base.OnDataBinding (e);
  354. }
  355. #if NET_2_0
  356. protected virtual void OnDataPropertyChanged ()
  357. {
  358. if (Initialized)
  359. RequiresDataBinding = true;
  360. }
  361. protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
  362. {
  363. RequiresDataBinding = true;
  364. }
  365. protected internal override void OnInit (EventArgs e)
  366. {
  367. base.OnInit (e);
  368. if (Page != null) {
  369. Page.PreLoad += new EventHandler (OnPagePreLoad);
  370. if (!IsViewStateEnabled && Page.IsPostBack)
  371. RequiresDataBinding = true;
  372. }
  373. }
  374. protected virtual void OnPagePreLoad (object sender, EventArgs e)
  375. {
  376. Initialize ();
  377. }
  378. protected internal override void OnLoad (EventArgs e)
  379. {
  380. if (!Initialized)
  381. Initialize ();
  382. base.OnLoad (e);
  383. }
  384. private void Initialize ()
  385. {
  386. if (Page != null) {
  387. if (!Page.IsPostBack || (IsViewStateEnabled && !IsDataBound))
  388. RequiresDataBinding = true;
  389. }
  390. if (IsBoundUsingDataSourceID)
  391. ConnectToDataSource ();
  392. initialized = true;
  393. }
  394. protected internal override void OnPreRender (EventArgs e)
  395. {
  396. EnsureDataBound ();
  397. base.OnPreRender (e);
  398. }
  399. #endif
  400. protected virtual void OnSelectedIndexChanged (EventArgs e)
  401. {
  402. EventHandler selectedIndexChanged = (EventHandler) Events [selectedIndexChangedEvent];
  403. if (selectedIndexChanged != null)
  404. selectedIndexChanged (this, e);
  405. }
  406. protected abstract void PrepareControlHierarchy ();
  407. #if NET_2_0
  408. protected internal
  409. #else
  410. protected
  411. #endif
  412. override void Render (HtmlTextWriter writer)
  413. {
  414. PrepareControlHierarchy ();
  415. // don't call base class or RenderBegin|EndTag
  416. // or we'll get an extra <span></span>
  417. RenderContents (writer);
  418. }
  419. [WebSysDescription("")]
  420. [WebCategory("Action")]
  421. public event EventHandler SelectedIndexChanged {
  422. add { Events.AddHandler (selectedIndexChangedEvent, value); }
  423. remove { Events.RemoveHandler (selectedIndexChangedEvent, value); }
  424. }
  425. static public bool IsBindableType (Type type)
  426. {
  427. // I can't believe how many NRE are possible in System.Web
  428. if (type == null) // Type.GetTypeCode no longer throws when a null is passed.
  429. throw new NullReferenceException ();
  430. switch (Type.GetTypeCode (type)) {
  431. case TypeCode.Boolean:
  432. case TypeCode.Byte:
  433. case TypeCode.SByte:
  434. case TypeCode.Int16:
  435. case TypeCode.UInt16:
  436. case TypeCode.Int32:
  437. case TypeCode.UInt32:
  438. case TypeCode.Int64:
  439. case TypeCode.UInt64:
  440. case TypeCode.Char:
  441. case TypeCode.Double:
  442. case TypeCode.Single:
  443. case TypeCode.DateTime:
  444. case TypeCode.Decimal:
  445. case TypeCode.String:
  446. return true;
  447. default:
  448. return false;
  449. }
  450. }
  451. #if NET_2_0
  452. void ConnectToDataSource ()
  453. {
  454. if (NamingContainer != null)
  455. boundDataSource = (NamingContainer.FindControl (DataSourceID) as IDataSource);
  456. if (boundDataSource == null) {
  457. if (Parent != null)
  458. boundDataSource = (Parent.FindControl (DataSourceID) as IDataSource);
  459. if (boundDataSource == null)
  460. throw new HttpException (Locale.GetText ("Coulnd't find a DataSource named '{0}'.", DataSourceID));
  461. }
  462. DataSourceView dsv = boundDataSource.GetView (String.Empty);
  463. dsv.DataSourceViewChanged += new EventHandler (OnDataSourceViewChanged);
  464. }
  465. #endif
  466. }
  467. }