DataBoundControl.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // System.Web.UI.WebControls.DataBoundControl
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  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.Collections.Specialized;
  32. using System.Text;
  33. using System.Web.Util;
  34. using System.ComponentModel;
  35. namespace System.Web.UI.WebControls {
  36. public abstract class DataBoundControl : WebControl
  37. {
  38. public event EventHandler DataBound;
  39. protected DataBoundControl ()
  40. {
  41. }
  42. public sealed override void DataBind ()
  43. {
  44. PerformDataBinding ();
  45. RequiresDataBinding = false;
  46. OnDataBound (EventArgs.Empty);
  47. }
  48. protected void EnsureDataBound ()
  49. {
  50. if (RequiresDataBinding && DataSourceID != "")
  51. DataBind ();
  52. }
  53. protected virtual object GetDataSourceObject()
  54. {
  55. if (DataSourceID != "")
  56. return (IDataSource) NamingContainer.FindControl (DataSourceID);
  57. return DataSource;
  58. }
  59. protected virtual IEnumerable GetResolvedDataSource ()
  60. {
  61. if (DataSource != null && DataSourceID != "")
  62. throw new HttpException ();
  63. IDataSource ds = GetDataSourceObject () as IDataSource;
  64. if (ds != null && DataSourceID != "")
  65. return ds.GetView (DataMember).Select ();
  66. else if (DataSource != null)
  67. return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
  68. else
  69. return null;
  70. }
  71. protected bool IsBoundToDataSourceControl()
  72. {
  73. return (GetDataSourceObject () is IDataSource) && DataSourceID != "";
  74. }
  75. protected virtual void OnDataBound(EventArgs e) {
  76. if (DataBound != null)
  77. DataBound (this, e);
  78. }
  79. protected virtual void OnDataPropertyChanged ()
  80. {
  81. this.RequiresDataBinding = true;
  82. }
  83. protected virtual void OnDataSourceChanged (object sender, EventArgs e)
  84. {
  85. RequiresDataBinding = true;
  86. }
  87. // should be `internal protected' (why, oh WHY did they do that !?!)
  88. protected override void OnInit (EventArgs e)
  89. {
  90. base.OnInit(e);
  91. inited = true;
  92. if (!Page.IsPostBack)
  93. RequiresDataBinding = true;
  94. }
  95. // should be `internal protected' (why, oh WHY did they do that !?!)
  96. protected override void OnLoad (EventArgs e)
  97. {
  98. IDataSource ds = GetDataSourceObject () as IDataSource;
  99. if (ds != null && DataSourceID != "")
  100. ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
  101. base.OnLoad(e);
  102. }
  103. // should be `internal protected' (why, oh WHY did they do that !?!)
  104. protected override void OnPreRender (EventArgs e)
  105. {
  106. EnsureDataBound ();
  107. base.OnPreRender (e);
  108. }
  109. protected virtual void PerformDataBinding ()
  110. {
  111. OnDataBinding(EventArgs.Empty);
  112. }
  113. protected virtual void ValidateDataSource (object dataSource)
  114. {
  115. if (dataSource is IListSource || dataSource is IEnumerable)
  116. return;
  117. throw new ArgumentException ();
  118. }
  119. public string DataMember
  120. {
  121. get {
  122. object o = ViewState["DataMember"];
  123. if(o!=null)
  124. return (string)o;
  125. return String.Empty;
  126. }
  127. set {
  128. ViewState["DataMember"] = value;
  129. }
  130. }
  131. object dataSource;
  132. public virtual object DataSource
  133. {
  134. get {
  135. return dataSource;
  136. }
  137. set {
  138. ValidateDataSource (value);
  139. dataSource = value;
  140. }
  141. }
  142. public virtual string DataSourceID {
  143. get {
  144. object o = ViewState ["DataSourceID"];
  145. if (o != null)
  146. return (string)o;
  147. return String.Empty;
  148. }
  149. set {
  150. if (inited)
  151. RequiresDataBinding = true;
  152. ViewState ["DataSourceID"] = value;
  153. }
  154. }
  155. bool requiresDataBinding;
  156. protected bool RequiresDataBinding {
  157. get { return requiresDataBinding; }
  158. set { requiresDataBinding = value; }
  159. }
  160. protected bool inited;
  161. }
  162. }
  163. #endif