DropDownList.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: DropDownList
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2002)
  12. */
  13. using System;
  14. using System.Collections.Specialized;
  15. using System.Web;
  16. using System.Web.UI;
  17. namespace System.Web.UI.WebControls
  18. {
  19. public class DropDownList : ListControl, IPostBackDataHandler
  20. {
  21. public DropDownList(): base()
  22. {
  23. }
  24. public override Color BorderColor
  25. {
  26. get
  27. {
  28. return BorderColor;
  29. }
  30. set
  31. {
  32. BorderColor = value;
  33. }
  34. }
  35. public override BorderStyle BorderStyle
  36. {
  37. get
  38. {
  39. return BorderStyle;
  40. }
  41. set
  42. {
  43. BorderStyle = value;
  44. }
  45. }
  46. public override Unit BorderWidth
  47. {
  48. get
  49. {
  50. return BorderWidth;
  51. }
  52. set
  53. {
  54. BorderWidth = value;
  55. }
  56. }
  57. public override int SelectedIndex
  58. {
  59. get
  60. {
  61. return SelectedIndex;
  62. }
  63. set
  64. {
  65. SelectedIndex = value;
  66. }
  67. }
  68. public override string ToolTip
  69. {
  70. get
  71. {
  72. return ToolTip;
  73. }
  74. set
  75. {
  76. ToolTip = value;
  77. }
  78. }
  79. protected override void AddAttributesToRender(HtmlTextWriter writer)
  80. {
  81. if(Page != null)
  82. {
  83. Page.VerifyRenderingInServerForm(this);
  84. }
  85. writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
  86. AddAttributesToRender(writer);
  87. if(AutoPostBack && Page != null)
  88. {
  89. writer.AddAttribute(HtmlTextWriterAttribute.Onchange, Page.GetPostBackClientEvent(this,""));
  90. writer.AddAttribute("language", "javascript");
  91. }
  92. }
  93. protected override ControlCollection CreateControlCollection()
  94. {
  95. return new EmptyControlCollection(this);
  96. }
  97. protected override void RenderContents(HtmlTextWriter writer)
  98. {
  99. if(Items != null)
  100. {
  101. bool selected = false;
  102. foreach(ListItem current in Items)
  103. {
  104. writer.WriteBeginTag("option");
  105. if(Selected)
  106. {
  107. if(selected)
  108. {
  109. throw new HttpException(HttpRuntime.FormatResourceString("Cannot_Multiselect_In_DropDownList"));
  110. }
  111. selected = true;
  112. writer.WriteAttribute("selected", "selected", false);
  113. }
  114. writer.WriteAttribute("value", current.Value, true);
  115. writer.Write('>');
  116. HttpUtility.Encode(current.Text, writer);
  117. writer.WriteEndTag("option");
  118. writer.WriteLine();
  119. }
  120. }
  121. }
  122. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
  123. {
  124. string[] vals = postCollection.GetValues();
  125. if(vals != null)
  126. {
  127. int index = Items.FindByValueInternal(vals[0]);
  128. if(index != SelectedIndex)
  129. {
  130. SelectedIndex = index;
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. void IPostBackDataHandler.RaisePostDataChangedEvent()
  137. {
  138. OnSelectedIndexChanged(EventArgs.Empty);
  139. }
  140. }
  141. }