BaseDataBoundControl.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. ValidateDataSource (value);
  65. dataSource = value;
  66. if (initialized)
  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. if (initialized)
  79. OnDataPropertyChanged ();
  80. }
  81. }
  82. protected bool Initialized {
  83. get { return initialized; }
  84. }
  85. protected bool IsBoundUsingDataSourceID {
  86. get { return DataSourceID.Length > 0; }
  87. }
  88. protected bool RequiresDataBinding {
  89. get { return requiresDataBinding; }
  90. set {
  91. requiresDataBinding = value;
  92. if (value && preRendered && IsBoundUsingDataSourceID && Page != null && !Page.IsCallback)
  93. EnsureDataBound ();
  94. }
  95. }
  96. protected void ConfirmInitState ()
  97. {
  98. initialized = true;
  99. }
  100. public override void DataBind ()
  101. {
  102. RequiresDataBinding = false;
  103. PerformSelect ();
  104. }
  105. protected virtual void EnsureDataBound ()
  106. {
  107. if (RequiresDataBinding && IsBoundUsingDataSourceID)
  108. DataBind ();
  109. }
  110. protected virtual void OnDataBound (EventArgs e)
  111. {
  112. if (DataBound != null)
  113. DataBound (this, e);
  114. }
  115. protected virtual void OnDataPropertyChanged ()
  116. {
  117. RequiresDataBinding = true;
  118. }
  119. protected internal override void OnInit (EventArgs e)
  120. {
  121. base.OnInit (e);
  122. Page.PreLoad += new EventHandler (OnPagePreLoad);
  123. }
  124. protected virtual void OnPagePreLoad (object sender, EventArgs e)
  125. {
  126. ConfirmInitState ();
  127. }
  128. protected internal override void OnPreRender (EventArgs e)
  129. {
  130. preRendered = true;
  131. EnsureDataBound ();
  132. base.OnPreRender (e);
  133. }
  134. protected abstract void PerformSelect ();
  135. protected abstract void ValidateDataSource (object dataSource);
  136. }
  137. }
  138. #endif