2
0

DropDownList.cs 3.0 KB

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