DataBoundControl.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. namespace System.Web.UI.WebControls {
  39. // CAS
  40. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  42. // attributes
  43. [DesignerAttribute ("System.Web.UI.Design.WebControls.DataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  44. public abstract class DataBoundControl : BaseDataBoundControl
  45. {
  46. DataSourceSelectArguments selectArguments;
  47. DataSourceView currentView;
  48. protected DataBoundControl ()
  49. {
  50. }
  51. /* Used for controls that used to inherit from
  52. * WebControl, so the tag can propagate upwards
  53. */
  54. internal DataBoundControl (HtmlTextWriterTag tag) : base (tag)
  55. {
  56. }
  57. protected virtual IDataSource GetDataSource ()
  58. {
  59. if (IsBoundUsingDataSourceID) {
  60. Control namingContainer;
  61. Control ctrl = null;
  62. namingContainer = NamingContainer;
  63. while (namingContainer != null) {
  64. ctrl = namingContainer.FindControl (DataSourceID);
  65. if (ctrl != null)
  66. break;
  67. namingContainer = namingContainer.NamingContainer;
  68. }
  69. if (ctrl == null)
  70. throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
  71. if (!(ctrl is IDataSource))
  72. throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IDataSource.", DataSourceID));
  73. return (IDataSource) ctrl;
  74. }
  75. IDataSource ds = DataSource as IDataSource;
  76. if (ds != null) return ds;
  77. IEnumerable ie = DataSourceResolver.ResolveDataSource (DataSource, DataMember);
  78. return new CollectionDataSource (ie);
  79. }
  80. protected virtual DataSourceView GetData ()
  81. {
  82. if (currentView == null)
  83. UpdateViewData ();
  84. return currentView;
  85. }
  86. DataSourceView InternalGetData ()
  87. {
  88. if (DataSource != null && IsBoundUsingDataSourceID)
  89. throw new HttpException ("Control bound using both DataSourceID and DataSource properties.");
  90. IDataSource ds = GetDataSource ();
  91. if (ds != null)
  92. return ds.GetView (DataMember);
  93. else
  94. return null;
  95. }
  96. protected override void OnDataPropertyChanged ()
  97. {
  98. base.OnDataPropertyChanged ();
  99. UpdateViewData ();
  100. }
  101. protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
  102. {
  103. RequiresDataBinding = true;
  104. }
  105. protected override void OnPagePreLoad (object sender, EventArgs e)
  106. {
  107. base.OnPagePreLoad (sender, e);
  108. UpdateViewData ();
  109. }
  110. void UpdateViewData ()
  111. {
  112. DataSourceView view = InternalGetData ();
  113. if (view == currentView) return;
  114. if (currentView != null)
  115. currentView.DataSourceViewChanged -= new EventHandler (OnDataSourceViewChanged);
  116. currentView = view;
  117. if (view != null)
  118. view.DataSourceViewChanged += new EventHandler (OnDataSourceViewChanged);
  119. }
  120. protected internal override void OnLoad (EventArgs e)
  121. {
  122. if (!Page.IsPostBack || (IsViewStateEnabled && !IsDataBound))
  123. RequiresDataBinding = true;
  124. base.OnLoad(e);
  125. }
  126. protected internal virtual void PerformDataBinding (IEnumerable data)
  127. {
  128. }
  129. protected override void ValidateDataSource (object dataSource)
  130. {
  131. if (dataSource is IListSource || dataSource is IEnumerable || dataSource is IDataSource)
  132. return;
  133. throw new ArgumentException ("Invalid data source source type. The data source must be of type IListSource, IEnumerable or IDataSource.");
  134. }
  135. [ThemeableAttribute (false)]
  136. [DefaultValueAttribute ("")]
  137. [WebCategoryAttribute ("Data")]
  138. public virtual string DataMember {
  139. get { return ViewState.GetString ("DataMember", ""); }
  140. set { ViewState["DataMember"] = value; }
  141. }
  142. [IDReferencePropertyAttribute (typeof(DataSourceControl))]
  143. public override string DataSourceID {
  144. get { return ViewState.GetString ("DataSourceID", ""); }
  145. set {
  146. ViewState ["DataSourceID"] = value;
  147. base.DataSourceID = value;
  148. }
  149. }
  150. //
  151. // See DataBoundControl.MarkAsDataBound msdn doc for the code example
  152. //
  153. protected override void PerformSelect ()
  154. {
  155. // Call OnDataBinding here if bound to a data source using the
  156. // DataSource property (instead of a DataSourceID), because the
  157. // databinding statement is evaluated before the call to GetData.
  158. if (!IsBoundUsingDataSourceID)
  159. OnDataBinding (EventArgs.Empty);
  160. SelectArguments = CreateDataSourceSelectArguments ();
  161. GetData ().Select (SelectArguments, new DataSourceViewSelectCallback (OnSelect));
  162. MarkAsDataBound ();
  163. // Raise the DataBound event.
  164. OnDataBound (EventArgs.Empty);
  165. }
  166. void OnSelect (IEnumerable data)
  167. {
  168. // Call OnDataBinding only if it has not already been
  169. // called in the PerformSelect method.
  170. if (IsBoundUsingDataSourceID) {
  171. OnDataBinding (EventArgs.Empty);
  172. }
  173. // The PerformDataBinding method binds the data in the
  174. // retrievedData collection to elements of the data-bound control.
  175. PerformDataBinding (data);
  176. }
  177. protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
  178. {
  179. return DataSourceSelectArguments.Empty;
  180. }
  181. protected DataSourceSelectArguments SelectArguments {
  182. get {
  183. if (selectArguments == null)
  184. selectArguments = CreateDataSourceSelectArguments ();
  185. return selectArguments;
  186. }
  187. private set {
  188. selectArguments = value;
  189. }
  190. }
  191. bool IsDataBound {
  192. get {
  193. object dataBound = ViewState ["DataBound"];
  194. return dataBound != null ? (bool) dataBound : false;
  195. }
  196. set {
  197. ViewState ["DataBound"] = value;
  198. }
  199. }
  200. protected void MarkAsDataBound ()
  201. {
  202. IsDataBound = true;
  203. }
  204. }
  205. }
  206. #endif