2
0

ListDataHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Project : Mono
  23. * Namespace : System.Web.Mobile.Util
  24. * Class : ListDataHelper
  25. * Author : Gaurav Vaish
  26. *
  27. * Copyright : 2003 with Gaurav Vaish, and with
  28. * Ximian Inc
  29. */
  30. using System.Collections;
  31. using System.Web.UI;
  32. using System.Web.Mobile;
  33. using System.Web.UI.MobileControls;
  34. namespace System.Web.Mobile.Util
  35. {
  36. internal class ListDataHelper
  37. {
  38. private object dataSource;
  39. private int dataSourceCount = -1;
  40. private IEnumerable resolvedDataSrc;
  41. private MobileListItemCollection items;
  42. private string dataTextField;
  43. private string dataValueField;
  44. private bool bindFromFields;
  45. private IListControl parent;
  46. private StateBag parentViewState;
  47. public ListDataHelper(IListControl parent, StateBag parentViewState)
  48. {
  49. this.parent = parent;
  50. this.parentViewState = parentViewState;
  51. }
  52. public string DataMember
  53. {
  54. get
  55. {
  56. object o = parentViewState["DataMember"];
  57. if(o != null)
  58. return (string)o;
  59. return String.Empty;
  60. }
  61. set
  62. {
  63. parentViewState["DataMember"] = value;
  64. }
  65. }
  66. public string DataTextField
  67. {
  68. get
  69. {
  70. object o = parentViewState["DataTextField"];
  71. if(o != null)
  72. return (string)o;
  73. return String.Empty;
  74. }
  75. set
  76. {
  77. parentViewState["DataTextField"] = value;
  78. }
  79. }
  80. public string DataValueField
  81. {
  82. get
  83. {
  84. object o = parentViewState["DataValueField"];
  85. if(o != null)
  86. return (string)o;
  87. return String.Empty;
  88. }
  89. set
  90. {
  91. parentViewState["DataValueField"] = value;
  92. }
  93. }
  94. public object DataSource
  95. {
  96. get
  97. {
  98. return this.dataSource;
  99. }
  100. set
  101. {
  102. this.dataSource = value;
  103. }
  104. }
  105. public int DataSourceCount
  106. {
  107. get
  108. {
  109. if(dataSourceCount == -1)
  110. {
  111. if(ResolvedDataSource != null)
  112. {
  113. if(ResolvedDataSource is ICollection)
  114. dataSourceCount = ((ICollection)ResolvedDataSource).Count;
  115. }
  116. }
  117. return dataSourceCount;
  118. }
  119. }
  120. public IEnumerable ResolvedDataSource
  121. {
  122. get
  123. {
  124. if(this.resolvedDataSrc == null)
  125. {
  126. resolvedDataSrc = DataSourceHelper.GetResolvedDataSource(DataSource, DataMember);
  127. }
  128. return resolvedDataSrc;
  129. }
  130. }
  131. public MobileListItemCollection Items
  132. {
  133. get
  134. {
  135. if(items == null)
  136. {
  137. items = new MobileListItemCollection();
  138. if(parent.TrackingViewState)
  139. ((IStateManager)items).TrackViewState();
  140. }
  141. return items;
  142. }
  143. }
  144. public void AddItem(MobileListItem item)
  145. {
  146. Items.Add(item);
  147. }
  148. public MobileListItem CreateItem(object dataItem)
  149. {
  150. MobileListItem retVal;
  151. string itemText = null;
  152. string itemValue = null;
  153. if(bindFromFields)
  154. {
  155. if(this.dataTextField.Length > 0)
  156. {
  157. itemText = DataBinder.GetPropertyValue(dataItem,
  158. dataTextField, "{0}");
  159. }
  160. if(this.dataValueField.Length > 0)
  161. {
  162. itemValue = DataBinder.GetPropertyValue(dataItem,
  163. dataValueField, "{0}");
  164. }
  165. } else
  166. {
  167. itemText = dataItem.ToString();
  168. }
  169. retVal = new MobileListItem(dataItem, itemText, itemValue);
  170. if(dataItem != null)
  171. {
  172. parent.OnItemDataBind(new ListDataBindEventArgs(retVal, dataItem));
  173. }
  174. return retVal;
  175. }
  176. }
  177. }