DataBoundControl.cs 3.6 KB

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