DataBoundControl.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // System.Web.UI.WebControls.DataBoundControl
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Sanjay Gupta ([email protected])
  7. //
  8. // (C) 2003 Ben Maurer
  9. // (C) 2004 Novell, Inc. (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Text;
  35. using System.Web.Util;
  36. using System.ComponentModel;
  37. using System.Security.Permissions;
  38. using System.Web.UI.WebControls.Adapters;
  39. namespace System.Web.UI.WebControls {
  40. // CAS
  41. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  42. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  43. // attributes
  44. [DesignerAttribute ("System.Web.UI.Design.WebControls.DataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  45. public abstract class DataBoundControl : BaseDataBoundControl
  46. {
  47. DataSourceSelectArguments selectArguments;
  48. DataSourceView currentView;
  49. protected DataBoundControl ()
  50. {
  51. }
  52. /* Used for controls that used to inherit from
  53. * WebControl, so the tag can propagate upwards
  54. */
  55. internal DataBoundControl (HtmlTextWriterTag tag) : base (tag)
  56. {
  57. }
  58. protected virtual IDataSource GetDataSource ()
  59. {
  60. if (IsBoundUsingDataSourceID) {
  61. Control ctrl = FindDataSource ();
  62. if (ctrl == null)
  63. throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
  64. if (!(ctrl is IDataSource))
  65. throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IDataSource.", DataSourceID));
  66. return (IDataSource) ctrl;
  67. }
  68. IDataSource ds = DataSource as IDataSource;
  69. if (ds != null) return ds;
  70. IEnumerable ie = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
  71. return new CollectionDataSource (ie);
  72. }
  73. protected virtual DataSourceView GetData ()
  74. {
  75. if (currentView == null)
  76. UpdateViewData ();
  77. return currentView;
  78. }
  79. DataSourceView InternalGetData ()
  80. {
  81. if (currentView != null)
  82. return currentView;
  83. if (DataSource != null && IsBoundUsingDataSourceID)
  84. throw new HttpException ("Control bound using both DataSourceID and DataSource properties.");
  85. IDataSource ds = GetDataSource ();
  86. if (ds != null)
  87. return ds.GetView (DataMember);
  88. else
  89. return null;
  90. }
  91. protected override void OnDataPropertyChanged ()
  92. {
  93. base.OnDataPropertyChanged ();
  94. currentView = null;
  95. }
  96. protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
  97. {
  98. RequiresDataBinding = true;
  99. }
  100. // MSDN: The OnPagePreLoad method is overridden by the DataBoundControl class
  101. // to set the BaseDataBoundControl.RequiresDataBinding property to true in
  102. // cases where the HTTP request is a postback and view state is enabled but
  103. // the data-bound control has not yet been bound.
  104. //
  105. // LAMESPEC: RequiresDataBinding is also set when http request is NOT a postback -
  106. // no matter whether view state is enabled. The correct description should be:
  107. //
  108. // The OnPagePreLoad method is override by the DataBoundControl class
  109. // to set the BaseDataBoundControl.RequiresDataBinding property to true in
  110. // cases where the HTTP request is not a postback or it is a postback and view state
  111. // is enabled but the data-bound control has not yet been bound.
  112. protected override void OnPagePreLoad (object sender, EventArgs e)
  113. {
  114. base.OnPagePreLoad (sender, e);
  115. Initialize ();
  116. }
  117. void Initialize ()
  118. {
  119. Page page = Page;
  120. if (page != null) {
  121. // LAMESPEC: see the comment above OnPagePreLoad
  122. if (!page.IsPostBack || (!IsDataBound && IsViewStateEnabled))
  123. RequiresDataBinding = true;
  124. }
  125. }
  126. void UpdateViewData ()
  127. {
  128. if (currentView != null)
  129. currentView.DataSourceViewChanged -= new EventHandler (OnDataSourceViewChanged);
  130. DataSourceView view = InternalGetData ();
  131. if (view != currentView)
  132. currentView = view;
  133. if (currentView != null)
  134. currentView.DataSourceViewChanged += new EventHandler (OnDataSourceViewChanged);
  135. }
  136. protected internal override void OnLoad (EventArgs e)
  137. {
  138. UpdateViewData ();
  139. if (!Initialized) {
  140. Initialize ();
  141. // MSDN: The ConfirmInitState method sets the initialized state of the data-bound
  142. // control. The method is called by the DataBoundControl class in its OnLoad
  143. // method.
  144. ConfirmInitState ();
  145. }
  146. base.OnLoad(e);
  147. }
  148. protected internal virtual void PerformDataBinding (IEnumerable data)
  149. {
  150. }
  151. protected override void ValidateDataSource (object dataSource)
  152. {
  153. if (dataSource == null || dataSource is IListSource || dataSource is IEnumerable || dataSource is IDataSource)
  154. return;
  155. throw new ArgumentException ("Invalid data source source type. The data source must be of type IListSource, IEnumerable or IDataSource.");
  156. }
  157. [ThemeableAttribute (false)]
  158. [DefaultValueAttribute ("")]
  159. [WebCategoryAttribute ("Data")]
  160. public virtual string DataMember {
  161. get { return ViewState.GetString ("DataMember", ""); }
  162. set { ViewState["DataMember"] = value; }
  163. }
  164. [IDReferencePropertyAttribute (typeof(DataSourceControl))]
  165. public override string DataSourceID {
  166. get { return ViewState.GetString ("DataSourceID", ""); }
  167. set {
  168. ViewState ["DataSourceID"] = value;
  169. base.DataSourceID = value;
  170. }
  171. }
  172. #if NET_4_0
  173. [Browsable (false)]
  174. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  175. public IDataSource DataSourceObject {
  176. get { return GetDataSource (); }
  177. }
  178. #endif
  179. //
  180. // See DataBoundControl.MarkAsDataBound msdn doc for the code example
  181. //
  182. protected override void PerformSelect ()
  183. {
  184. // Call OnDataBinding here if bound to a data source using the
  185. // DataSource property (instead of a DataSourceID), because the
  186. // databinding statement is evaluated before the call to GetData.
  187. if (!IsBoundUsingDataSourceID)
  188. OnDataBinding (EventArgs.Empty);
  189. // prevent recursive calls
  190. RequiresDataBinding = false;
  191. SelectArguments = CreateDataSourceSelectArguments ();
  192. GetData ().Select (SelectArguments, new DataSourceViewSelectCallback (OnSelect));
  193. // The PerformDataBinding method has completed.
  194. MarkAsDataBound ();
  195. // Raise the DataBound event.
  196. OnDataBound (EventArgs.Empty);
  197. }
  198. void OnSelect (IEnumerable data)
  199. {
  200. // Call OnDataBinding only if it has not already been
  201. // called in the PerformSelect method.
  202. if (IsBoundUsingDataSourceID) {
  203. OnDataBinding (EventArgs.Empty);
  204. }
  205. // The PerformDataBinding method binds the data in the
  206. // retrievedData collection to elements of the data-bound control.
  207. InternalPerformDataBinding (data);
  208. }
  209. #if NET_4_0
  210. internal
  211. #else
  212. protected
  213. #endif
  214. void InternalPerformDataBinding (IEnumerable data)
  215. {
  216. DataBoundControlAdapter adapter = Adapter as DataBoundControlAdapter;
  217. if (adapter != null)
  218. adapter.PerformDataBinding (data);
  219. else
  220. PerformDataBinding (data);
  221. }
  222. protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
  223. {
  224. return DataSourceSelectArguments.Empty;
  225. }
  226. protected DataSourceSelectArguments SelectArguments {
  227. get {
  228. if (selectArguments == null)
  229. selectArguments = CreateDataSourceSelectArguments ();
  230. return selectArguments;
  231. }
  232. private set {
  233. selectArguments = value;
  234. }
  235. }
  236. bool IsDataBound {
  237. get {
  238. object dataBound = ViewState ["DataBound"];
  239. return dataBound != null ? (bool) dataBound : false;
  240. }
  241. set {
  242. ViewState ["DataBound"] = value;
  243. }
  244. }
  245. protected void MarkAsDataBound ()
  246. {
  247. IsDataBound = true;
  248. }
  249. }
  250. }
  251. #endif