HtmlContainerControl.cs 3.6 KB

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