DropDownList.cs 3.0 KB

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