ListDataHelper.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * Project : Mono
  3. * Namespace : System.Web.Mobile.Util
  4. * Class : ListDataHelper
  5. * Author : Gaurav Vaish
  6. *
  7. * Copyright : 2003 with Gaurav Vaish, and with
  8. * Ximian Inc
  9. */
  10. using System.Collections;
  11. using System.Web.UI;
  12. using System.Web.Mobile;
  13. using System.Web.UI.MobileControls;
  14. namespace System.Web.Mobile.Util
  15. {
  16. internal class ListDataHelper
  17. {
  18. private object dataSource;
  19. private int dataSourceCount = -1;
  20. private IEnumerable resolvedDataSrc;
  21. private MobileListItemCollection items;
  22. private string dataTextField;
  23. private string dataValueField;
  24. private bool bindFromFields;
  25. private IListControl parent;
  26. private StateBag parentViewState;
  27. public ListDataHelper(IListControl parent, StateBag parentViewState)
  28. {
  29. this.parent = parent;
  30. this.parentViewState = parentViewState;
  31. }
  32. public string DataMember
  33. {
  34. get
  35. {
  36. object o = parentViewState["DataMember"];
  37. if(o != null)
  38. return (string)o;
  39. return String.Empty;
  40. }
  41. set
  42. {
  43. parentViewState["DataMember"] = value;
  44. }
  45. }
  46. public string DataTextField
  47. {
  48. get
  49. {
  50. object o = parentViewState["DataTextField"];
  51. if(o != null)
  52. return (string)o;
  53. return String.Empty;
  54. }
  55. set
  56. {
  57. parentViewState["DataTextField"] = value;
  58. }
  59. }
  60. public string DataValueField
  61. {
  62. get
  63. {
  64. object o = parentViewState["DataValueField"];
  65. if(o != null)
  66. return (string)o;
  67. return String.Empty;
  68. }
  69. set
  70. {
  71. parentViewState["DataValueField"] = value;
  72. }
  73. }
  74. public object DataSource
  75. {
  76. get
  77. {
  78. return this.dataSource;
  79. }
  80. set
  81. {
  82. this.dataSource = value;
  83. }
  84. }
  85. public int DataSourceCount
  86. {
  87. get
  88. {
  89. if(dataSourceCount == -1)
  90. {
  91. if(ResolvedDataSource != null)
  92. {
  93. if(ResolvedDataSource is ICollection)
  94. dataSourceCount = ((ICollection)ResolvedDataSource).Count;
  95. }
  96. }
  97. return dataSourceCount;
  98. }
  99. }
  100. public IEnumerable ResolvedDataSource
  101. {
  102. get
  103. {
  104. if(this.resolvedDataSrc == null)
  105. {
  106. resolvedDataSrc = DataSourceHelper.GetResolvedDataSource(DataSource, DataMember);
  107. }
  108. return resolvedDataSrc;
  109. }
  110. }
  111. public MobileListItemCollection Items
  112. {
  113. get
  114. {
  115. if(items == null)
  116. {
  117. items = new MobileListItemCollection();
  118. if(parent.TrackingViewState)
  119. ((IStateManager)items).TrackViewState();
  120. }
  121. return items;
  122. }
  123. }
  124. public void AddItem(MobileListItem item)
  125. {
  126. Items.Add(item);
  127. }
  128. public MobileListItem CreateItem(object dataItem)
  129. {
  130. MobileListItem retVal;
  131. string itemText = null;
  132. string itemValue = null;
  133. if(bindFromFields)
  134. {
  135. if(this.dataTextField.Length > 0)
  136. {
  137. itemText = DataBinder.GetPropertyValue(dataItem,
  138. dataTextField, "{0}");
  139. }
  140. if(this.dataValueField.Length > 0)
  141. {
  142. itemValue = DataBinder.GetPropertyValue(dataItem,
  143. dataValueField, "{0}");
  144. }
  145. } else
  146. {
  147. itemText = dataItem.ToString();
  148. }
  149. retVal = new MobileListItem(dataItem, itemText, itemValue);
  150. if(dataItem != null)
  151. {
  152. parent.OnItemDataBind(new ListDataBindEventArgs(retVal, dataItem));
  153. }
  154. return retVal;
  155. }
  156. }
  157. }