Panel.cs 8.1 KB

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