HierarchicalDataBoundControl.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // System.Web.UI.WebControls.HierarchicalDataBoundControl.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. using System.Web.UI;
  33. using System.Web.UI.WebControls.Adapters;
  34. namespace System.Web.UI.WebControls
  35. {
  36. [DesignerAttribute ("System.Web.UI.Design.WebControls.HierarchicalDataBoundControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  37. public abstract class HierarchicalDataBoundControl : BaseDataBoundControl
  38. {
  39. [IDReferencePropertyAttribute (typeof(HierarchicalDataSourceControl))]
  40. public override string DataSourceID {
  41. get {
  42. object o = ViewState ["DataSourceID"];
  43. if (o != null)
  44. return (string)o;
  45. return String.Empty;
  46. }
  47. set {
  48. if (Initialized)
  49. RequiresDataBinding = true;
  50. ViewState ["DataSourceID"] = value;
  51. }
  52. }
  53. protected virtual HierarchicalDataSourceView GetData (string viewPath)
  54. {
  55. if (DataSource != null && DataSourceID != "")
  56. throw new HttpException ();
  57. IHierarchicalDataSource ds = GetDataSource ();
  58. if (ds != null)
  59. return ds.GetHierarchicalView (viewPath);
  60. if (DataSource is IHierarchicalEnumerable)
  61. return new ReadOnlyDataSourceView ((IHierarchicalEnumerable) DataSource);
  62. return null;
  63. }
  64. protected virtual IHierarchicalDataSource GetDataSource ()
  65. {
  66. if (IsBoundUsingDataSourceID) {
  67. Control ctrl = FindDataSource ();
  68. if (ctrl == null)
  69. throw new HttpException (string.Format ("A control with ID '{0}' could not be found.", DataSourceID));
  70. if (!(ctrl is IHierarchicalDataSource))
  71. throw new HttpException (string.Format ("The control with ID '{0}' is not a control of type IHierarchicalDataSource.", DataSourceID));
  72. return (IHierarchicalDataSource) ctrl;
  73. }
  74. return DataSource as IHierarchicalDataSource;
  75. }
  76. bool IsDataBound {
  77. get {
  78. return ViewState.GetBool ("DataBound", false);
  79. }
  80. set {
  81. ViewState ["DataBound"] = value;
  82. }
  83. }
  84. protected void MarkAsDataBound ()
  85. {
  86. IsDataBound = true;
  87. }
  88. protected override void OnDataPropertyChanged ()
  89. {
  90. RequiresDataBinding = true;
  91. }
  92. protected virtual void OnDataSourceChanged (object sender, EventArgs e)
  93. {
  94. RequiresDataBinding = true;
  95. }
  96. protected internal override void OnLoad (EventArgs e)
  97. {
  98. if (!Initialized) {
  99. Initialize ();
  100. ConfirmInitState ();
  101. }
  102. base.OnLoad(e);
  103. }
  104. private void Initialize ()
  105. {
  106. if (!Page.IsPostBack || (IsViewStateEnabled && !IsDataBound))
  107. RequiresDataBinding = true;
  108. IHierarchicalDataSource ds = GetDataSource ();
  109. if (ds != null && DataSourceID != "")
  110. ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
  111. }
  112. protected override void OnPagePreLoad (object sender, EventArgs e)
  113. {
  114. base.OnPagePreLoad (sender, e);
  115. Initialize ();
  116. }
  117. protected void InternalPerformDataBinding ()
  118. {
  119. HierarchicalDataBoundControlAdapter adapter
  120. = Adapter as HierarchicalDataBoundControlAdapter;
  121. if (adapter != null)
  122. adapter.PerformDataBinding ();
  123. else
  124. PerformDataBinding ();
  125. }
  126. protected internal virtual void PerformDataBinding ()
  127. {
  128. }
  129. protected override void PerformSelect ()
  130. {
  131. OnDataBinding (EventArgs.Empty);
  132. InternalPerformDataBinding ();
  133. // The PerformDataBinding method has completed.
  134. RequiresDataBinding = false;
  135. MarkAsDataBound ();
  136. OnDataBound (EventArgs.Empty);
  137. }
  138. protected override void ValidateDataSource (object dataSource)
  139. {
  140. if (dataSource == null || dataSource is IHierarchicalDataSource || dataSource is IHierarchicalEnumerable)
  141. return;
  142. throw new InvalidOperationException ("Invalid data source");
  143. }
  144. }
  145. }
  146. #endif