HtmlContainerControl.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.ComponentModel;
  32. using System.Security.Permissions;
  33. using System.Text;
  34. //LAMESPEC: The dox talk about HttpException but are very ambigious.
  35. //TODO: Check to see if Render really is overridden instead of a LiteralControl being added. It apears that this is the
  36. //case due to testing. Anything inside the block is overwritten by the content of this control, so it doesnt apear
  37. //to do anything with children.
  38. //TODO: If Test.InnerText = Test.InnerHtml without ever assigning anything into InnerHtml, you get this:
  39. // Exception Details: System.Web.HttpException: Cannot get inner content of Message because the contents are not literal.
  40. //[HttpException (0x80004005): Cannot get inner content of Message because the contents are not literal.]
  41. // System.Web.UI.HtmlControls.HtmlContainerControl.get_InnerHtml() +278
  42. // ASP.test3_aspx.AnchorBtn_Click(Object Source, EventArgs E) in \\genfs2\www24\bobsmith11\test3.aspx:6
  43. // System.Web.UI.HtmlControls.HtmlAnchor.OnServerClick(EventArgs e) +108
  44. // System.Web.UI.HtmlControls.HtmlAnchor.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +26
  45. // System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
  46. // System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +149
  47. // System.Web.UI.Page.ProcessRequestMain() +660
  48. namespace System.Web.UI.HtmlControls
  49. {
  50. // CAS
  51. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  52. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  53. public abstract class HtmlContainerControl : HtmlControl {
  54. #if NET_2_0
  55. protected
  56. #else
  57. public
  58. #endif
  59. HtmlContainerControl () : this ("span") {}
  60. public HtmlContainerControl (string tag) : base(tag) {}
  61. [HtmlControlPersistable (false)]
  62. [BrowsableAttribute(false)]
  63. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  64. public virtual string InnerHtml
  65. {
  66. get {
  67. if (Controls.Count == 0)
  68. return String.Empty;
  69. if (Controls.Count == 1) {
  70. Control ctrl = Controls [0];
  71. LiteralControl lc = ctrl as LiteralControl;
  72. if (lc != null)
  73. return lc.Text;
  74. DataBoundLiteralControl dblc = ctrl as DataBoundLiteralControl;
  75. if (dblc != null)
  76. return dblc.Text;
  77. }
  78. throw new HttpException ("There is no literal content!");
  79. }
  80. set {
  81. Controls.Clear ();
  82. Controls.Add (new LiteralControl (value));
  83. if (value == null)
  84. ViewState.Remove ("innerhtml");
  85. else
  86. ViewState ["innerhtml"] = value;
  87. }
  88. }
  89. [HtmlControlPersistable (false)]
  90. [BrowsableAttribute(false)]
  91. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  92. public virtual string InnerText
  93. {
  94. get {
  95. return HttpUtility.HtmlDecode (InnerHtml);
  96. }
  97. set {
  98. InnerHtml = HttpUtility.HtmlEncode (value);
  99. }
  100. }
  101. #if NET_2_0
  102. protected internal
  103. #else
  104. protected
  105. #endif
  106. override void Render (HtmlTextWriter writer)
  107. {
  108. RenderBeginTag (writer);
  109. RenderChildren (writer);
  110. RenderEndTag (writer);
  111. }
  112. protected virtual void RenderEndTag (HtmlTextWriter writer)
  113. {
  114. writer.WriteEndTag (TagName);
  115. }
  116. protected override void RenderAttributes (HtmlTextWriter writer)
  117. {
  118. ViewState.Remove ("innerhtml");
  119. base.RenderAttributes (writer);
  120. }
  121. /* we need to override this because our base class
  122. * (HtmlControl) returns an instance of
  123. * EmptyControlCollection. */
  124. protected override ControlCollection CreateControlCollection ()
  125. {
  126. return new ControlCollection (this);
  127. }
  128. protected override void LoadViewState (object savedState)
  129. {
  130. if (savedState != null) {
  131. base.LoadViewState (savedState);
  132. string inner = ViewState ["innerhtml"] as string;
  133. if (inner != null)
  134. InnerHtml = inner;
  135. }
  136. }
  137. }
  138. }