BaseDataBoundControl.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // System.Web.UI.WebControls.BaseDataBoundControl.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) 2004-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. #if NET_2_0
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.Security.Permissions;
  32. namespace System.Web.UI.WebControls {
  33. // CAS
  34. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  35. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. // attributes
  37. [DefaultProperty ("DataSourceID")]
  38. [DesignerAttribute ("System.Web.UI.Design.WebControls.BaseDataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  39. public abstract class BaseDataBoundControl: WebControl
  40. {
  41. public event EventHandler DataBound;
  42. object dataSource;
  43. bool initialized;
  44. bool preRendered;
  45. bool requiresDataBinding;
  46. protected BaseDataBoundControl ()
  47. {
  48. }
  49. /* Used for controls that used to inherit from
  50. * WebControl, so the tag can propagate upwards
  51. */
  52. internal BaseDataBoundControl (HtmlTextWriterTag tag) : base (tag)
  53. {
  54. }
  55. [BindableAttribute (true)]
  56. [ThemeableAttribute (false)]
  57. [DefaultValueAttribute (null)]
  58. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  59. public virtual object DataSource {
  60. get {
  61. return dataSource;
  62. }
  63. set {
  64. if(value!=null)
  65. ValidateDataSource (value);
  66. dataSource = value;
  67. OnDataPropertyChanged ();
  68. }
  69. }
  70. [DefaultValueAttribute ("")]
  71. [ThemeableAttribute (false)]
  72. public virtual string DataSourceID {
  73. get {
  74. return ViewState.GetString ("DataSourceID", "");
  75. }
  76. set {
  77. ViewState["DataSourceID"] = value;
  78. OnDataPropertyChanged ();
  79. }
  80. }
  81. protected bool Initialized {
  82. get { return initialized; }
  83. }
  84. protected bool IsBoundUsingDataSourceID {
  85. get { return DataSourceID.Length > 0; }
  86. }
  87. protected bool RequiresDataBinding {
  88. get { return requiresDataBinding; }
  89. set {
  90. // MSDN: If you set the RequiresDataBinding
  91. // property to true when the data-bound control
  92. // has already begun to render its output to the
  93. // page, the current HTTP request is not a
  94. // callback, and you are using the DataSourceID
  95. // property to identify the data source control
  96. // to bind to, the DataBind method is called
  97. // immediately. In this case, the
  98. // RequiresDataBinding property is _not_ actually
  99. // set to true.
  100. //
  101. // LAMESPEC, the docs quoted above mention that
  102. // DataBind is called in the described
  103. // case. This is wrong since that way we don't
  104. // break recursion when the property is set from
  105. // within the OnSelect handler in user's
  106. // code. EnsureDataBound makes sure that no
  107. // recursive binding is performed. Also the
  108. // property DOES get set in this case (according
  109. // to tests)
  110. if (value && preRendered && IsBoundUsingDataSourceID && Page != null && !Page.IsCallback) {
  111. requiresDataBinding = true;
  112. EnsureDataBound ();
  113. } else
  114. requiresDataBinding = value;
  115. }
  116. }
  117. protected void ConfirmInitState ()
  118. {
  119. initialized = true;
  120. }
  121. public override void DataBind ()
  122. {
  123. PerformSelect ();
  124. }
  125. protected virtual void EnsureDataBound ()
  126. {
  127. if (RequiresDataBinding && IsBoundUsingDataSourceID)
  128. DataBind ();
  129. }
  130. protected virtual void OnDataBound (EventArgs e)
  131. {
  132. if (DataBound != null)
  133. DataBound (this, e);
  134. }
  135. protected virtual void OnDataPropertyChanged ()
  136. {
  137. if (Initialized)
  138. RequiresDataBinding = true;
  139. }
  140. protected internal override void OnInit (EventArgs e)
  141. {
  142. base.OnInit (e);
  143. Page.PreLoad += new EventHandler (OnPagePreLoad);
  144. if (!IsViewStateEnabled && Page != null && Page.IsPostBack)
  145. RequiresDataBinding = true;
  146. }
  147. protected virtual void OnPagePreLoad (object sender, EventArgs e)
  148. {
  149. ConfirmInitState ();
  150. }
  151. protected internal override void OnPreRender (EventArgs e)
  152. {
  153. preRendered = true;
  154. EnsureDataBound ();
  155. base.OnPreRender (e);
  156. }
  157. internal Control FindDataSource ()
  158. {
  159. Control ctrl = null;
  160. Control namingContainer = NamingContainer;
  161. while (namingContainer != null) {
  162. ctrl = namingContainer.FindControl (DataSourceID);
  163. if (ctrl != null)
  164. break;
  165. namingContainer = namingContainer.NamingContainer;
  166. }
  167. return ctrl;
  168. }
  169. protected abstract void PerformSelect ();
  170. protected abstract void ValidateDataSource (object dataSource);
  171. }
  172. }
  173. #endif