HtmlContainerControl.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. public HtmlContainerControl () : this ("span") {}
  35. public HtmlContainerControl (string tag) : base(tag) {}
  36. [HtmlControlPersistable (false)]
  37. [BrowsableAttribute(false)]
  38. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  39. public virtual string InnerHtml
  40. {
  41. get {
  42. if (Controls.Count == 0)
  43. return String.Empty;
  44. bool is_literal = true;
  45. StringBuilder text = new StringBuilder ();
  46. foreach (Control ctrl in Controls) {
  47. LiteralControl lc = ctrl as LiteralControl;
  48. if (lc == null) {
  49. is_literal = false;
  50. break;
  51. }
  52. text.Append (lc.Text);
  53. }
  54. if (!is_literal)
  55. throw new HttpException ("There is no literal content!");
  56. return text.ToString ();
  57. }
  58. set {
  59. Controls.Clear ();
  60. Controls.Add (new LiteralControl (value));
  61. ViewState ["innerhtml"] = value;
  62. }
  63. }
  64. [HtmlControlPersistable (false)]
  65. [BrowsableAttribute(false)]
  66. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  67. public virtual string InnerText
  68. {
  69. get {
  70. return HttpUtility.HtmlDecode (InnerHtml);
  71. }
  72. set {
  73. InnerHtml = HttpUtility.HtmlEncode (value);
  74. }
  75. }
  76. protected override void Render (HtmlTextWriter writer)
  77. {
  78. RenderBeginTag (writer);
  79. RenderChildren (writer);
  80. RenderEndTag (writer);
  81. }
  82. protected virtual void RenderEndTag (HtmlTextWriter writer)
  83. {
  84. writer.WriteEndTag (TagName);
  85. }
  86. protected override void RenderAttributes (HtmlTextWriter writer)
  87. {
  88. ViewState.Remove ("innerhtml");
  89. base.RenderAttributes (writer);
  90. }
  91. protected override ControlCollection CreateControlCollection ()
  92. {
  93. return new ControlCollection (this);
  94. }
  95. protected override void LoadViewState (object savedState)
  96. {
  97. if (savedState != null) {
  98. base.LoadViewState (savedState);
  99. string inner = ViewState ["innerhtml"] as string;
  100. if (inner != null)
  101. InnerHtml = inner;
  102. }
  103. }
  104. }
  105. }