BaseDataBoundControl.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // System.Web.UI.WebControls.BaseDataBoundControl.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2004 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.Collections;
  31. using System.ComponentModel;
  32. namespace System.Web.UI.WebControls
  33. {
  34. [DefaultProperty ("DataSourceID")]
  35. [DesignerAttribute ("System.Web.UI.Design.WebControls.BaseDataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  36. public abstract class BaseDataBoundControl: WebControl
  37. {
  38. public event EventHandler DataBound;
  39. object dataSource;
  40. string dataSourceId;
  41. bool initialized;
  42. bool requiresDataBinding;
  43. protected BaseDataBoundControl ()
  44. {
  45. }
  46. /* Used for controls that used to inherit from
  47. * WebControl, so the tag can propagate upwards
  48. */
  49. internal BaseDataBoundControl (HtmlTextWriterTag tag) : base (tag)
  50. {
  51. }
  52. [BindableAttribute (true)]
  53. [ThemeableAttribute (false)]
  54. [DefaultValueAttribute (null)]
  55. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  56. public virtual object DataSource {
  57. get {
  58. return dataSource;
  59. }
  60. set {
  61. ValidateDataSource (value);
  62. dataSource = value;
  63. if (initialized)
  64. OnDataPropertyChanged ();
  65. }
  66. }
  67. [DefaultValueAttribute ("")]
  68. [ThemeableAttribute (false)]
  69. public virtual string DataSourceID {
  70. get {
  71. return dataSourceId != null ? dataSourceId : string.Empty;
  72. }
  73. set {
  74. dataSourceId = value;
  75. if (initialized)
  76. OnDataPropertyChanged ();
  77. }
  78. }
  79. protected bool Initialized {
  80. get { return initialized; }
  81. }
  82. protected bool IsBoundUsingDataSourceID {
  83. get { return DataSourceID.Length > 0; }
  84. }
  85. protected bool RequiresDataBinding {
  86. get { return requiresDataBinding; }
  87. set { requiresDataBinding = value; }
  88. }
  89. protected void ConfirmInitState ()
  90. {
  91. initialized = true;
  92. }
  93. public override void DataBind ()
  94. {
  95. RequiresDataBinding = false;
  96. PerformSelect ();
  97. }
  98. protected virtual void EnsureDataBound ()
  99. {
  100. if (RequiresDataBinding && IsBoundUsingDataSourceID)
  101. DataBind ();
  102. }
  103. protected virtual void OnDataBound (EventArgs e)
  104. {
  105. if (DataBound != null)
  106. DataBound (this, e);
  107. }
  108. protected virtual void OnDataPropertyChanged ()
  109. {
  110. RequiresDataBinding = true;
  111. }
  112. protected internal override void OnInit (EventArgs e)
  113. {
  114. base.OnInit (e);
  115. Page.PreLoad += new EventHandler (OnPagePreLoad);
  116. }
  117. protected virtual void OnPagePreLoad (object sender, EventArgs e)
  118. {
  119. ConfirmInitState ();
  120. }
  121. protected internal override void OnPreRender (EventArgs e)
  122. {
  123. EnsureDataBound ();
  124. base.OnPreRender (e);
  125. }
  126. protected abstract void PerformSelect ();
  127. protected abstract void ValidateDataSource (object dataSource);
  128. }
  129. }
  130. #endif