Panel.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // System.Web.UI.WebControls.Panel.cs
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // TODO: Are we missing something in LoadViewState?
  10. // What to do in AddParsedSubObject
  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. namespace System.Web.UI.WebControls {
  34. // CAS
  35. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  36. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. // attributes
  38. [Designer ("System.Web.UI.Design.WebControls.PanelDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  39. [ParseChildren (false)]
  40. [PersistChildren (true)]
  41. public class Panel : WebControl {
  42. public Panel () : base (HtmlTextWriterTag.Div)
  43. {
  44. }
  45. protected override void AddAttributesToRender (HtmlTextWriter w)
  46. {
  47. base.AddAttributesToRender (w);
  48. string image = BackImageUrl;
  49. if (image != "") {
  50. image = ResolveClientUrl (image);
  51. #if !NET_2_0 // see HtmlTextWriter.WriteStyleAttribute(string, string, bool)
  52. image = String.Concat ("url(", image, ")");
  53. #endif
  54. w.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, image);
  55. }
  56. #if NET_2_0
  57. if (!String.IsNullOrEmpty (DefaultButton) && Page != null) {
  58. Control button = FindControl (DefaultButton);
  59. if (button == null || !(button is IButtonControl))
  60. throw new InvalidOperationException (String.Format ("The DefaultButton of '{0}' must be the ID of a control of type IButtonControl.", ID));
  61. Page.ClientScript.RegisterWebFormClientScript ();
  62. w.AddAttribute ("onkeypress",
  63. "javascript:return " + Page.WebFormScriptReference + ".WebForm_FireDefaultButton(event, '" + button.ClientID + "')");
  64. }
  65. if (Direction != ContentDirection.NotSet) {
  66. w.AddAttribute (HtmlTextWriterAttribute.Dir, Direction == ContentDirection.RightToLeft ? "rtl" : "ltr", false);
  67. }
  68. switch (ScrollBars) {
  69. case ScrollBars.Auto:
  70. w.AddStyleAttribute (HtmlTextWriterStyle.Overflow, "auto");
  71. break;
  72. case ScrollBars.Both:
  73. w.AddStyleAttribute (HtmlTextWriterStyle.Overflow, "scroll");
  74. break;
  75. case ScrollBars.Horizontal:
  76. w.AddStyleAttribute (HtmlTextWriterStyle.OverflowX, "scroll");
  77. break;
  78. case ScrollBars.Vertical:
  79. w.AddStyleAttribute (HtmlTextWriterStyle.OverflowY, "scroll");
  80. break;
  81. }
  82. #endif
  83. if (!Wrap) {
  84. #if NET_2_0
  85. w.AddStyleAttribute (HtmlTextWriterStyle.WhiteSpace, "nowrap");
  86. #else
  87. w.AddAttribute (HtmlTextWriterAttribute.Nowrap, "nowrap");
  88. #endif
  89. }
  90. string align = "";
  91. switch (HorizontalAlign) {
  92. case HorizontalAlign.Center: align = "center"; break;
  93. case HorizontalAlign.Justify: align = "justify"; break;
  94. case HorizontalAlign.Left: align = "left"; break;
  95. case HorizontalAlign.Right: align = "right"; break;
  96. }
  97. if (align != "")
  98. #if NET_2_0
  99. w.AddStyleAttribute (HtmlTextWriterStyle.TextAlign, align);
  100. #else
  101. w.AddAttribute (HtmlTextWriterAttribute.Align, align);
  102. #endif
  103. }
  104. #if NET_2_0
  105. PanelStyle PanelStyle {
  106. get { return (ControlStyle as PanelStyle); }
  107. }
  108. [UrlProperty]
  109. [DefaultValue ("")]
  110. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  111. [WebSysDescription ("")]
  112. [WebCategory ("Appearance")]
  113. public virtual string BackImageUrl {
  114. get {
  115. if (ControlStyleCreated) {
  116. if (PanelStyle != null)
  117. return PanelStyle.BackImageUrl;
  118. else
  119. return ViewState.GetString ("BackImageUrl", String.Empty);
  120. }
  121. return String.Empty;
  122. }
  123. set {
  124. if(PanelStyle!=null)
  125. PanelStyle.BackImageUrl = value;
  126. else
  127. ViewState ["BackImageUrl"] = value;
  128. }
  129. }
  130. [DefaultValue (HorizontalAlign.NotSet)]
  131. [WebSysDescription ("")]
  132. [WebCategory ("Layout")]
  133. public virtual HorizontalAlign HorizontalAlign {
  134. get {
  135. if (ControlStyleCreated) {
  136. if (PanelStyle != null)
  137. return PanelStyle.HorizontalAlign;
  138. else
  139. return ViewState ["HorizontalAlign"] != null ? (HorizontalAlign) ViewState ["HorizontalAlign"] : HorizontalAlign.NotSet;
  140. }
  141. return HorizontalAlign.NotSet;
  142. }
  143. set {
  144. if (PanelStyle != null)
  145. PanelStyle.HorizontalAlign = value;
  146. else
  147. ViewState ["HorizontalAlign"] = value;
  148. }
  149. }
  150. [DefaultValue (true)]
  151. [WebSysDescription ("")]
  152. [WebCategory ("Layout")]
  153. public virtual bool Wrap {
  154. get {
  155. if (ControlStyleCreated) {
  156. if (PanelStyle != null)
  157. return PanelStyle.Wrap;
  158. else
  159. return ViewState.GetBool ("Wrap", true);
  160. }
  161. return true;
  162. }
  163. set {
  164. if (PanelStyle != null)
  165. PanelStyle.Wrap = value;
  166. else
  167. ViewState ["Wrap"] = value;
  168. }
  169. }
  170. [ThemeableAttribute (false)]
  171. [DefaultValue ("")]
  172. public virtual string DefaultButton {
  173. get {
  174. return ViewState.GetString ("DefaultButton", String.Empty);
  175. }
  176. set {
  177. ViewState ["DefaultButton"] = value;
  178. }
  179. }
  180. [DefaultValue (ContentDirection.NotSet)]
  181. public virtual ContentDirection Direction {
  182. get {
  183. if (ControlStyleCreated) {
  184. if (PanelStyle != null)
  185. return PanelStyle.Direction;
  186. else
  187. return ViewState ["Direction"] != null ? (ContentDirection) ViewState ["Direction"] : ContentDirection.NotSet;
  188. }
  189. return ContentDirection.NotSet;
  190. }
  191. set {
  192. if (PanelStyle != null)
  193. PanelStyle.Direction = value;
  194. else
  195. ViewState ["Direction"] = value;
  196. }
  197. }
  198. [LocalizableAttribute (true)]
  199. [DefaultValue ("")]
  200. public virtual string GroupingText {
  201. get {
  202. return ViewState.GetString ("GroupingText", String.Empty);
  203. }
  204. set {
  205. ViewState ["GroupingText"] = value;
  206. }
  207. }
  208. [DefaultValue (ScrollBars.None)]
  209. public virtual ScrollBars ScrollBars {
  210. get {
  211. if (ControlStyleCreated) {
  212. if (PanelStyle != null)
  213. return PanelStyle.ScrollBars;
  214. else
  215. return ViewState ["ScrollBars"] != null ? (ScrollBars) ViewState ["Direction"] : ScrollBars.None;
  216. }
  217. return ScrollBars.None;
  218. }
  219. set {
  220. if (PanelStyle != null)
  221. PanelStyle.ScrollBars = value;
  222. else
  223. ViewState ["ScrollBars"] = value;
  224. }
  225. }
  226. #if NET_4_0
  227. public override bool SupportsDisabledAttribute {
  228. get { return RenderingCompatibilityLessThan40; }
  229. }
  230. #endif
  231. protected override Style CreateControlStyle ()
  232. {
  233. return new PanelStyle (ViewState);
  234. }
  235. public override void RenderBeginTag (HtmlTextWriter writer)
  236. {
  237. base.RenderBeginTag (writer);
  238. if (!String.IsNullOrEmpty (GroupingText)) {
  239. writer.RenderBeginTag (HtmlTextWriterTag.Fieldset);
  240. writer.RenderBeginTag (HtmlTextWriterTag.Legend);
  241. writer.Write (GroupingText);
  242. writer.RenderEndTag ();
  243. }
  244. }
  245. public override void RenderEndTag (HtmlTextWriter writer)
  246. {
  247. if (!String.IsNullOrEmpty (GroupingText)) {
  248. writer.RenderEndTag (); // Fieldset
  249. }
  250. base.RenderEndTag (writer);
  251. }
  252. #endif
  253. }
  254. }