DataBoundControl.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // System.Web.UI.WebControls.DataBoundControl
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Sanjay Gupta ([email protected])
  7. //
  8. // (C) 2003 Ben Maurer
  9. // (C) 2004 Novell, Inc. (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Text;
  35. using System.Web.Util;
  36. using System.ComponentModel;
  37. namespace System.Web.UI.WebControls {
  38. public abstract class DataBoundControl : BaseDataBoundControl
  39. {
  40. public event EventHandler DataBound;
  41. protected DataBoundControl ()
  42. {
  43. }
  44. public sealed override void DataBind ()
  45. {
  46. PerformDataBinding ();
  47. RequiresDataBinding = false;
  48. OnDataBound (EventArgs.Empty);
  49. }
  50. protected void EnsureDataBound ()
  51. {
  52. if (RequiresDataBinding && DataSourceID != "")
  53. DataBind ();
  54. }
  55. protected virtual object GetDataSourceObject()
  56. {
  57. if (DataSourceID != "")
  58. return (IDataSource) NamingContainer.FindControl (DataSourceID);
  59. return DataSource;
  60. }
  61. protected virtual IEnumerable GetResolvedDataSource ()
  62. {
  63. if (DataSource != null && DataSourceID != "")
  64. throw new HttpException ();
  65. IDataSource ds = GetDataSourceObject () as IDataSource;
  66. if (ds != null && DataSourceID != "")
  67. return ds.GetView (DataMember).ExecuteSelect (selectArguments);
  68. else if (DataSource != null)
  69. return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
  70. else
  71. return null;
  72. }
  73. protected bool IsBoundToDataSourceControl()
  74. {
  75. return (GetDataSourceObject () is IDataSource) && DataSourceID != "";
  76. }
  77. protected virtual void OnDataBound(EventArgs e) {
  78. if (DataBound != null)
  79. DataBound (this, e);
  80. }
  81. protected virtual void OnDataPropertyChanged ()
  82. {
  83. this.RequiresDataBinding = true;
  84. }
  85. protected virtual void OnDataSourceChanged (object sender, EventArgs e)
  86. {
  87. RequiresDataBinding = true;
  88. }
  89. // should be `internal protected' (why, oh WHY did they do that !?!)
  90. protected override void OnInit (EventArgs e)
  91. {
  92. base.OnInit(e);
  93. inited = true;
  94. if (!Page.IsPostBack)
  95. RequiresDataBinding = true;
  96. }
  97. // should be `internal protected' (why, oh WHY did they do that !?!)
  98. protected override void OnLoad (EventArgs e)
  99. {
  100. IDataSource ds = GetDataSourceObject () as IDataSource;
  101. if (ds != null && DataSourceID != "")
  102. ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
  103. base.OnLoad(e);
  104. }
  105. // should be `internal protected' (why, oh WHY did they do that !?!)
  106. protected override void OnPreRender (EventArgs e)
  107. {
  108. EnsureDataBound ();
  109. base.OnPreRender (e);
  110. }
  111. protected virtual void PerformDataBinding ()
  112. {
  113. OnDataBinding(EventArgs.Empty);
  114. }
  115. protected virtual void ValidateDataSource (object dataSource)
  116. {
  117. if (dataSource is IListSource || dataSource is IEnumerable)
  118. return;
  119. throw new ArgumentException ();
  120. }
  121. public string DataMember
  122. {
  123. get {
  124. object o = ViewState["DataMember"];
  125. if(o!=null)
  126. return (string)o;
  127. return String.Empty;
  128. }
  129. set {
  130. ViewState["DataMember"] = value;
  131. }
  132. }
  133. object dataSource;
  134. public virtual object DataSource
  135. {
  136. get {
  137. return dataSource;
  138. }
  139. set {
  140. ValidateDataSource (value);
  141. dataSource = value;
  142. }
  143. }
  144. public virtual string DataSourceID {
  145. get {
  146. object o = ViewState ["DataSourceID"];
  147. if (o != null)
  148. return (string)o;
  149. return String.Empty;
  150. }
  151. set {
  152. if (inited)
  153. RequiresDataBinding = true;
  154. ViewState ["DataSourceID"] = value;
  155. }
  156. }
  157. bool requiresDataBinding;
  158. protected bool RequiresDataBinding {
  159. get { return requiresDataBinding; }
  160. set { requiresDataBinding = value; }
  161. }
  162. protected bool inited;
  163. DataSourceSelectArguments selectArguments = null;
  164. protected DataSourceSelectArguments SelectArguments {
  165. get { return selectArguments; }
  166. }
  167. }
  168. }
  169. #endif