DataBoundControl.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. namespace System.Web.UI.WebControls {
  38. [DesignerAttribute ("System.Web.UI.Design.WebControls.DataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  39. public abstract class DataBoundControl : BaseDataBoundControl
  40. {
  41. DataSourceSelectArguments selectArguments;
  42. DataSourceView currentView;
  43. protected DataBoundControl ()
  44. {
  45. }
  46. /* Used for controls that used to inherit from
  47. * WebControl, so the tag can propagate upwards
  48. */
  49. internal DataBoundControl (HtmlTextWriterTag tag) : base (tag)
  50. {
  51. }
  52. protected virtual IDataSource GetDataSource ()
  53. {
  54. if (IsBoundUsingDataSourceID) {
  55. Control ctrl = NamingContainer.FindControl (DataSourceID);
  56. if (ctrl == null)
  57. throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
  58. if (!(ctrl is IDataSource))
  59. throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IDataSource.", DataSourceID));
  60. return (IDataSource) ctrl;
  61. }
  62. if (DataSource == null) return null;
  63. IDataSource ds = DataSource as IDataSource;
  64. if (ds != null) return ds;
  65. IEnumerable ie = DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
  66. if (ie != null) return new CollectionDataSource (ie);
  67. throw new HttpException (string.Format ("Unexpected data source type: {0}", DataSource.GetType()));
  68. }
  69. protected virtual DataSourceView GetData ()
  70. {
  71. if (currentView == null)
  72. UpdateViewData ();
  73. return currentView;
  74. }
  75. DataSourceView InternalGetData ()
  76. {
  77. if (DataSource != null && IsBoundUsingDataSourceID)
  78. throw new HttpException ("Control bound using both DataSourceID and DataSource properties.");
  79. IDataSource ds = GetDataSource ();
  80. if (ds != null)
  81. return ds.GetView (DataMember);
  82. else
  83. return null;
  84. }
  85. protected override void OnDataPropertyChanged ()
  86. {
  87. base.OnDataPropertyChanged ();
  88. UpdateViewData ();
  89. }
  90. protected virtual void OnDataSourceViewChanged (object sender, EventArgs e)
  91. {
  92. RequiresDataBinding = true;
  93. }
  94. protected override void OnPagePreLoad (object sender, EventArgs e)
  95. {
  96. base.OnPagePreLoad (sender, e);
  97. UpdateViewData ();
  98. }
  99. void UpdateViewData ()
  100. {
  101. DataSourceView view = InternalGetData ();
  102. if (view == currentView) return;
  103. if (currentView != null)
  104. currentView.DataSourceViewChanged -= new EventHandler (OnDataSourceViewChanged);
  105. currentView = view;
  106. if (view != null)
  107. view.DataSourceViewChanged += new EventHandler (OnDataSourceViewChanged);
  108. }
  109. protected internal override void OnLoad (EventArgs e)
  110. {
  111. if (IsBoundUsingDataSourceID && (!Page.IsPostBack || !EnableViewState))
  112. RequiresDataBinding = true;
  113. base.OnLoad(e);
  114. }
  115. protected internal virtual void PerformDataBinding (IEnumerable data)
  116. {
  117. }
  118. protected override void ValidateDataSource (object dataSource)
  119. {
  120. if (dataSource is IListSource || dataSource is IEnumerable || dataSource is IDataSource)
  121. return;
  122. throw new ArgumentException ("Invalid data source source type. The data source must be of type IListSource, IEnumerable or IDataSource.");
  123. }
  124. [ThemeableAttribute (false)]
  125. [DefaultValueAttribute ("")]
  126. [WebCategoryAttribute ("Data")]
  127. public virtual string DataMember
  128. {
  129. get {
  130. object o = ViewState["DataMember"];
  131. if(o!=null)
  132. return (string)o;
  133. return String.Empty;
  134. }
  135. set {
  136. ViewState["DataMember"] = value;
  137. }
  138. }
  139. [IDReferencePropertyAttribute (typeof(DataSourceControl))]
  140. public override string DataSourceID {
  141. get {
  142. object o = ViewState ["DataSourceID"];
  143. if (o != null)
  144. return (string)o;
  145. return String.Empty;
  146. }
  147. set {
  148. ViewState ["DataSourceID"] = value;
  149. base.DataSourceID = value;
  150. }
  151. }
  152. protected override void PerformSelect ()
  153. {
  154. OnDataBinding (EventArgs.Empty);
  155. DataSourceView view = GetData ();
  156. if (view != null)
  157. view.Select (SelectArguments, new DataSourceViewSelectCallback (OnSelect));
  158. }
  159. void OnSelect (IEnumerable data)
  160. {
  161. PerformDataBinding (data);
  162. OnDataBound (EventArgs.Empty);
  163. }
  164. protected virtual DataSourceSelectArguments CreateDataSourceSelectArguments ()
  165. {
  166. return DataSourceSelectArguments.Empty;
  167. }
  168. protected DataSourceSelectArguments SelectArguments {
  169. get {
  170. if (selectArguments == null)
  171. selectArguments = CreateDataSourceSelectArguments ();
  172. return selectArguments;
  173. }
  174. }
  175. [MonoTODO]
  176. protected void MarkAsDataBound ()
  177. {
  178. throw new NotImplementedException ();
  179. }
  180. }
  181. }
  182. #endif