DataBoundControl.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. protected DataBoundControl ()
  41. {
  42. }
  43. protected IDataSource GetDataSource ()
  44. {
  45. if (DataSourceID != "")
  46. return NamingContainer.FindControl (DataSourceID) as IDataSource;
  47. return DataSource as IDataSource;
  48. }
  49. protected DataSourceView GetData ()
  50. {
  51. if (DataSource != null && DataSourceID != "")
  52. throw new HttpException ();
  53. IDataSource ds = GetDataSource ();
  54. if (ds != null && DataSourceID != "")
  55. return ds.GetView (DataMember);
  56. else
  57. return null;
  58. }
  59. protected override void OnDataPropertyChanged ()
  60. {
  61. RequiresDataBinding = true;
  62. }
  63. protected virtual void OnDataSourceViewChanged (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 OnLoad (EventArgs e)
  69. {
  70. IDataSource ds = GetDataSource ();
  71. if (ds != null && DataSourceID != "")
  72. ds.DataSourceChanged += new EventHandler (OnDataSourceViewChanged);
  73. base.OnLoad(e);
  74. }
  75. protected virtual void PerformDataBinding (IEnumerable data)
  76. {
  77. OnDataBinding (EventArgs.Empty);
  78. }
  79. protected override void ValidateDataSource (object dataSource)
  80. {
  81. if (dataSource is IListSource || dataSource is IEnumerable)
  82. return;
  83. throw new ArgumentException ();
  84. }
  85. public string DataMember
  86. {
  87. get {
  88. object o = ViewState["DataMember"];
  89. if(o!=null)
  90. return (string)o;
  91. return String.Empty;
  92. }
  93. set {
  94. ViewState["DataMember"] = value;
  95. }
  96. }
  97. public override string DataSourceID {
  98. get {
  99. object o = ViewState ["DataSourceID"];
  100. if (o != null)
  101. return (string)o;
  102. return String.Empty;
  103. }
  104. set {
  105. if (Initialized)
  106. RequiresDataBinding = true;
  107. ViewState ["DataSourceID"] = value;
  108. }
  109. }
  110. protected override void PerformSelect ()
  111. {
  112. IEnumerable data = null;
  113. DataSourceView view = GetData ();
  114. if (view != null)
  115. data = view.ExecuteSelect (SelectArguments);
  116. else if (DataSource != null)
  117. data = DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
  118. PerformDataBinding (data);
  119. }
  120. [MonoTODO]
  121. protected DataSourceSelectArguments SelectArguments {
  122. get { return null; }
  123. }
  124. }
  125. }
  126. #endif