HtmlContainerControl.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // System.Web.UI.HtmlControls.HtmlContainerControl.cs
  3. //
  4. // Authors:
  5. // Bob Smith <[email protected]>
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) Bob Smith
  9. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  10. //
  11. using System;
  12. using System.ComponentModel;
  13. using System.Text;
  14. using System.Web;
  15. using System.Web.UI;
  16. //LAMESPEC: The dox talk about HttpException but are very ambigious.
  17. //TODO: Check to see if Render really is overridden instead of a LiteralControl being added. It apears that this is the
  18. //case due to testing. Anything inside the block is overwritten by the content of this control, so it doesnt apear
  19. //to do anything with children.
  20. // a doc references this. add? protected override ControlCollection CreateControlCollection();
  21. //TODO: If Test.InnerText = Test.InnerHtml without ever assigning anything into InnerHtml, you get this:
  22. // Exception Details: System.Web.HttpException: Cannot get inner content of Message because the contents are not literal.
  23. //[HttpException (0x80004005): Cannot get inner content of Message because the contents are not literal.]
  24. // System.Web.UI.HtmlControls.HtmlContainerControl.get_InnerHtml() +278
  25. // ASP.test3_aspx.AnchorBtn_Click(Object Source, EventArgs E) in \\genfs2\www24\bobsmith11\test3.aspx:6
  26. // System.Web.UI.HtmlControls.HtmlAnchor.OnServerClick(EventArgs e) +108
  27. // System.Web.UI.HtmlControls.HtmlAnchor.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +26
  28. // System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
  29. // System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +149
  30. // System.Web.UI.Page.ProcessRequestMain() +660
  31. namespace System.Web.UI.HtmlControls
  32. {
  33. public abstract class HtmlContainerControl : HtmlControl{
  34. #if NET_2_0
  35. protected
  36. #else
  37. public
  38. #endif
  39. HtmlContainerControl () : this ("span") {}
  40. public HtmlContainerControl (string tag) : base(tag) {}
  41. [HtmlControlPersistable (false)]
  42. [BrowsableAttribute(false)]
  43. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  44. public virtual string InnerHtml
  45. {
  46. get {
  47. if (Controls.Count == 0)
  48. return String.Empty;
  49. bool is_literal = true;
  50. StringBuilder text = new StringBuilder ();
  51. foreach (Control ctrl in Controls) {
  52. LiteralControl lc = ctrl as LiteralControl;
  53. if (lc == null) {
  54. is_literal = false;
  55. break;
  56. }
  57. text.Append (lc.Text);
  58. }
  59. if (!is_literal)
  60. throw new HttpException ("There is no literal content!");
  61. return text.ToString ();
  62. }
  63. set {
  64. Controls.Clear ();
  65. Controls.Add (new LiteralControl (value));
  66. if (value == null)
  67. ViewState.Remove ("innerhtml");
  68. else
  69. ViewState ["innerhtml"] = value;
  70. }
  71. }
  72. [HtmlControlPersistable (false)]
  73. [BrowsableAttribute(false)]
  74. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  75. public virtual string InnerText
  76. {
  77. get {
  78. return HttpUtility.HtmlDecode (InnerHtml);
  79. }
  80. set {
  81. InnerHtml = HttpUtility.HtmlEncode (value);
  82. }
  83. }
  84. #if NET_2_0
  85. protected internal
  86. #else
  87. protected
  88. #endif
  89. override void Render (HtmlTextWriter writer)
  90. {
  91. RenderBeginTag (writer);
  92. RenderChildren (writer);
  93. RenderEndTag (writer);
  94. }
  95. protected virtual void RenderEndTag (HtmlTextWriter writer)
  96. {
  97. writer.WriteEndTag (TagName);
  98. }
  99. protected override void RenderAttributes (HtmlTextWriter writer)
  100. {
  101. ViewState.Remove ("innerhtml");
  102. base.RenderAttributes (writer);
  103. }
  104. protected override ControlCollection CreateControlCollection ()
  105. {
  106. return new ControlCollection (this);
  107. }
  108. protected override void LoadViewState (object savedState)
  109. {
  110. if (savedState != null) {
  111. base.LoadViewState (savedState);
  112. string inner = ViewState ["innerhtml"] as string;
  113. if (inner != null)
  114. InnerHtml = inner;
  115. }
  116. }
  117. }
  118. }