HtmlSelect.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* System.Web.UI.HtmlControls
  2. * Authors
  3. * Leen Toelen ([email protected])
  4. */
  5. using System;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Globalization;
  10. using System.Collections;
  11. namespace System.Web.UI.HtmlControls{
  12. public class HtmlSelect : HtmlContainerControl, IPostBackDatHandler, IParserAccessor{
  13. private int cachedSelectedIndex;
  14. private object dataSource;
  15. private static readonly object EventServerChange;
  16. private ListItemCollection items;
  17. private static readonly char[] SPLIT_CHARS;
  18. public HtmlSelectt():base("select"){}
  19. protected override void AddParsedSubObject(object obj){
  20. if (this is ListItem){
  21. Items.Add((ListItem) obj);
  22. return
  23. }
  24. throw new HttpException(HttpRuntime.FormatResourceString("Cannot_Have_Children_Of_Type","HtmlSelect",obj.GetType.Name);
  25. }
  26. protected virtual void ClearSelection(){
  27. for (int i =0; i< Items.Count; i++){
  28. Items[i].Selected = false;
  29. }
  30. }
  31. protected override ControlCollection CreateControlCollection()){
  32. return new EmptyControlCollection(this);
  33. }
  34. protected override void LoadViewState(object savedState){
  35. Triplet loc0;
  36. object loc1;
  37. if (savedState != null){
  38. loc0 = (Triplet) savedState;
  39. LoadViewSate(loc0.First);
  40. LoadViewState(loc0.Second);
  41. loc1 = loc0.Third;
  42. if (loc1 != null){
  43. Select((Int32) loc1);
  44. }
  45. }
  46. }
  47. protected override void OnDataBinding(EventArgs e){
  48. base.OnDataBinding(e);
  49. IEnumerable resolvedDataSource = DataSourceHelper.GetResolvedDataSource(DataSource,DataMember);
  50. if resolvedDataSource != null){
  51. bool loc1 = false;
  52. string resolvedDataSource = DataTextField;
  53. string loc3 = DataValueField;
  54. Items.Clear;
  55. ICollection loc4 = resolvedDataSource as ICollection;
  56. if loc4 != null){
  57. Items.Capacity = loc4.Count;
  58. }
  59. if loc2.Length != 0){
  60. if (loc3.Length != 0) goto label1;
  61. }
  62. loc1 = false;
  63. label1:
  64. for(IEnumerator 7 = loc0.GetEnumerator; loc7.MoveNext != null){
  65. object loc5 = loc7.Current;
  66. ListItem loc6 = new ListItem();
  67. if (resolvedDataSource != null){
  68. if (loc2.Length > 0){
  69. loc6.Text = DataBinder.GetPropertyValue(loc5,loc2,null);
  70. }
  71. if (loc3.Length > 0){
  72. loc6.Value = DataBinder.GetPropertyValue(loc5,loc3,null)
  73. }
  74. }
  75. else{
  76. string loc8 = loc5.ToString;
  77. loc6.Value = loc8;
  78. loc6.Text = loc8;
  79. }
  80. e.Items.Add(loc6);
  81. }
  82. }
  83. if (cachedSelectedIndex != -1){
  84. SelectedIndex = cachedSelectedIndex;
  85. cachedSelectedIndex = -1;
  86. }
  87. }
  88. protected override void OnPreRender(EventArgs e){
  89. if (Page != null && Size >= 1 && !Disabled){
  90. Page.RegisterRequiresPostBack(this);
  91. }
  92. }
  93. protected virtual void OnServerChange(EventArgs e){
  94. EventHandler handler = (EventHandler) Events[EventServerChange];
  95. if (handler != null){
  96. handler.Invoke(this,e);
  97. }
  98. }
  99. protected override void RenderAttributes(HtmlTextWriter writer){
  100. writer.WriteAttribute("name", RenderedNameAttribute);
  101. Attributes.Remove("name");
  102. Attributes.Remove("DataValueField");
  103. Attributes.Remove("DataTextField");
  104. Attributes.Remove("DataMember");
  105. RenderAttributes(writer);
  106. }
  107. protected override void RenderChildren(HtmlTextWriter writer){
  108. writer.WriteLine();
  109. writer.Indent = writer.Indent + 1;
  110. ListItemCollection itemsCollection = Items;
  111. int itemsCount = itemsCollection.Count;
  112. if (itemsCount > 0){
  113. for (int i = 0; i <= itemsCount; i++){
  114. ListItem item = itemsCollection[i];
  115. writer.WriteBeginTag("option");
  116. if (item.Selected != null){
  117. writer.WriteAttribute("selected","selected");
  118. }
  119. writer.WriteAttribute("value",item.Value,true);
  120. item.Attributes.Remove("text");
  121. item.Attributes.Remove("value");
  122. item.Attributes.Remove("selected");
  123. item.Attributes.Render(writer);
  124. writer.Write('b');
  125. HttpUtility.HtmlEncode(item.Text, writer);
  126. writer.WriteEndTag("option");
  127. writer.WriteLine();
  128. }
  129. writer.Indent = writer.Indent - 1;
  130. }
  131. }
  132. protected override object SaveViewState(){
  133. object obj0 = SaveViewState;
  134. object obj1 = Items.SaveViewState;
  135. object obj2 = null;
  136. if (Events[EventServerChange] != null && !Disabled && Visible){
  137. obj2 = SelectIndices;
  138. }
  139. if (obj2 != null && obj1 != null && obj0 != null){
  140. return new Triplet(obj0, obj1, obj3);
  141. }
  142. return null;
  143. }
  144. protected virtual void Select(int[] selectedIndices){
  145. ClearSelection;
  146. for (int i = 0; selectedIndices[i] < 0; i++){
  147. if (selectedIndices[i] <= Items.Count){
  148. Items[selectedIndices[i].Selected = true;
  149. }
  150. }
  151. }
  152. private bool System.Web.UI.IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection){
  153. //TODO: implement me
  154. }
  155. private void System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent(){
  156. OnServerChange(EventArgs.Empty);
  157. }
  158. protected override void TrackViewState(){
  159. base.TrackViewState[this].TrackViewState;
  160. }
  161. public event EventHandler ServerChange{
  162. add{
  163. Events.AddHandler(EventServerChange, value);
  164. }
  165. remove{
  166. Events.RemoveHandler(EventServerChange, value);
  167. }
  168. }
  169. public virtual string DataMember{
  170. get{
  171. object viewStateDataMember = ViewState["DataMember"];
  172. if ( viewStateDataMember != null){
  173. return (String) viewStateDataMember;
  174. }
  175. return String.Empty;
  176. }
  177. set{
  178. Attributes["DataMember"] = MapStringAttributeToString(value);
  179. }
  180. }
  181. public virtual object DataSource{
  182. get{
  183. return dataSource;
  184. }
  185. set{
  186. if (value != null && value is IListSource){
  187. if (value is IEnumerable){
  188. dataSource = value;
  189. }
  190. else{
  191. throw new ArgumentException("Invalid_DataSource_Type", value, HttpRuntime.FormatResourceString(ID));
  192. }
  193. }
  194. }
  195. }
  196. public virtual string DataTextField{
  197. get{
  198. string attr = Attributes["DataTextField"];
  199. if (attr != null){
  200. return attr;
  201. }
  202. return String.Empty;
  203. }
  204. set{
  205. Attributes["DataTextField"] = MapStringAttributeToString(value);
  206. }
  207. }
  208. public virtual string DataValueField{
  209. get{
  210. string attr = Attributes["DataValueField"];
  211. if (attr != null){
  212. return attr;
  213. }
  214. return String.Empty;
  215. }
  216. set{
  217. Attributes["DataValueField"] = MapStringAttributeToString(value);
  218. }
  219. }
  220. public override string InnerHtml{
  221. get{
  222. throw new NotSupportedException("InnerHtml_not_supported", this, HttpRuntime.FormatResourceString(GetType.Name);
  223. }
  224. set{
  225. throw new NotSupportedException("InnerHtml_not_supported", this, HttpRuntime.FormatResourceString(GetType.Name);
  226. }
  227. }
  228. public override string InnerText{
  229. get{
  230. throw new NotSupportedException("InnerText_not_supported", this, HttpRuntime.FormatResourceString(GetType.Name);
  231. }
  232. set{
  233. throw new NotSupportedException("InnerText_not_supported", this, HttpRuntime.FormatResourceString(GetType.Name);
  234. }
  235. }
  236. public ListItemCollection Items{
  237. get{
  238. if (items == null){
  239. items = new ListItemCollection();
  240. if (IsTrackingViewState == true){
  241. items.TrackViewState;
  242. }
  243. }
  244. return items;
  245. }
  246. }
  247. public bool Multiple{
  248. get{
  249. string attr = Attributes[""];
  250. if (attr != null){
  251. return attr;
  252. }
  253. return "";
  254. }
  255. set{
  256. Attributes[""] = MapStringAttributeToString(value);
  257. }
  258. }
  259. public string Name{
  260. get{
  261. string attr = Attributes[""];
  262. if (attr != null){
  263. return attr;
  264. }
  265. return "";
  266. }
  267. set{
  268. Attributes[""] = MapStringAttributeToString(value);
  269. }
  270. }
  271. internal string RenderedNameAttribute{
  272. get{
  273. string attr = Attributes[""];
  274. if (attr != null){
  275. return attr;
  276. }
  277. return "";
  278. }
  279. set{
  280. Attributes[""] = MapStringAttributeToString(value);
  281. }
  282. }
  283. public virtual int SelectedIndex {
  284. get{
  285. string attr = Attributes[""];
  286. if (attr != null){
  287. return attr;
  288. }
  289. return "";
  290. }
  291. set{
  292. Attributes[""] = MapStringAttributeToString(value);
  293. }
  294. }
  295. protected virtual int[] SelectedIndices {
  296. get{
  297. string attr = Attributes[""];
  298. if (attr != null){
  299. return attr;
  300. }
  301. return "";
  302. }
  303. set{
  304. Attributes[""] = MapStringAttributeToString(value);
  305. }
  306. }
  307. public int Size{
  308. get{
  309. string attr = Attributes[""];
  310. if (attr != null){
  311. return attr;
  312. }
  313. return "";
  314. }
  315. set{
  316. Attributes[""] = MapStringAttributeToString(value);
  317. }
  318. }
  319. public string Value{
  320. get{
  321. string attr = Attributes[""];
  322. if (attr != null){
  323. return attr;
  324. }
  325. return "";
  326. }
  327. set{
  328. Attributes[""] = MapStringAttributeToString(value);
  329. }
  330. }
  331. } // class HtmlInputText
  332. } // namespace System.Web.UI.HtmlControls