Panel.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. [ToolboxData ("<{0}:Panel runat=server>Panel</{0}:Panel>")]
  42. public class Panel : WebControl {
  43. public Panel () : base (HtmlTextWriterTag.Div)
  44. {
  45. }
  46. protected override void AddAttributesToRender (HtmlTextWriter w)
  47. {
  48. base.AddAttributesToRender (w);
  49. string image = BackImageUrl;
  50. if (image != "") {
  51. image = ResolveClientUrl (image);
  52. #if !NET_2_0 // see HtmlTextWriter.WriteStyleAttribute(string, string, bool)
  53. image = String.Format ("url({0})", image);
  54. #endif
  55. w.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, image);
  56. }
  57. #if NET_2_0
  58. if (!String.IsNullOrEmpty (DefaultButton) && Page != null) {
  59. Control button = FindControl (DefaultButton);
  60. if (button == null || !(button is IButtonControl))
  61. throw new InvalidOperationException (String.Format ("The DefaultButton of '{0}' must be the ID of a control of type IButtonControl.", ID));
  62. Page.ClientScript.RegisterWebFormClientScript ();
  63. w.AddAttribute ("onkeypress", String.Format ("javascript:return {1}WebForm_FireDefaultButton(event, '{0}')", button.ClientID, Page.IsMultiForm ? Page.theForm + "." : null));
  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. [Bindable(true)]
  106. [DefaultValue("")]
  107. [Editor("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
  108. [WebSysDescription ("")]
  109. [WebCategory ("Appearance")]
  110. public virtual string BackImageUrl {
  111. get {
  112. return ViewState.GetString ("BackImageUrl", "");
  113. }
  114. set {
  115. ViewState ["BackImageUrl"] = value;
  116. }
  117. }
  118. [Bindable(true)]
  119. [DefaultValue(HorizontalAlign.NotSet)]
  120. [WebSysDescription ("")]
  121. [WebCategory ("Layout")]
  122. public virtual HorizontalAlign HorizontalAlign {
  123. get {
  124. return (HorizontalAlign) ViewState.GetInt ("HorizontalAlign", (int) HorizontalAlign.NotSet);
  125. }
  126. set {
  127. ViewState ["HorizontalAlign"] = (int) value;
  128. }
  129. }
  130. [Bindable(true)]
  131. [DefaultValue(true)]
  132. [WebSysDescription ("")]
  133. [WebCategory ("Layout")]
  134. public virtual bool Wrap {
  135. get {
  136. return ViewState.GetBool ("Wrap", true);
  137. }
  138. set {
  139. ViewState ["Wrap"] = value;
  140. }
  141. }
  142. #endif
  143. #if NET_2_0
  144. PanelStyle PanelStyle {
  145. get { return (ControlStyle as PanelStyle); }
  146. }
  147. [Bindable (true)]
  148. [DefaultValue ("")]
  149. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  150. [WebSysDescription ("")]
  151. [WebCategory ("Appearance")]
  152. public virtual string BackImageUrl {
  153. get {
  154. if (ControlStyleCreated) {
  155. if (PanelStyle != null)
  156. return PanelStyle.BackImageUrl;
  157. else
  158. return ViewState.GetString ("BackImageUrl", String.Empty);
  159. }
  160. return String.Empty;
  161. }
  162. set {
  163. if(PanelStyle!=null)
  164. PanelStyle.BackImageUrl = value;
  165. else
  166. ViewState ["BackImageUrl"] = value;
  167. }
  168. }
  169. [Bindable (true)]
  170. [DefaultValue (HorizontalAlign.NotSet)]
  171. [WebSysDescription ("")]
  172. [WebCategory ("Layout")]
  173. public virtual HorizontalAlign HorizontalAlign {
  174. get {
  175. if (ControlStyleCreated) {
  176. if (PanelStyle != null)
  177. return PanelStyle.HorizontalAlign;
  178. else
  179. return ViewState ["HorizontalAlign"] != null ? (HorizontalAlign) ViewState ["HorizontalAlign"] : HorizontalAlign.NotSet;
  180. }
  181. return HorizontalAlign.NotSet;
  182. }
  183. set {
  184. if (PanelStyle != null)
  185. PanelStyle.HorizontalAlign = value;
  186. else
  187. ViewState ["HorizontalAlign"] = value;
  188. }
  189. }
  190. [Bindable (true)]
  191. [DefaultValue (true)]
  192. [WebSysDescription ("")]
  193. [WebCategory ("Layout")]
  194. public virtual bool Wrap {
  195. get {
  196. if (ControlStyleCreated) {
  197. if (PanelStyle != null)
  198. return PanelStyle.Wrap;
  199. else
  200. return ViewState.GetBool ("Wrap", true);
  201. }
  202. return true;
  203. }
  204. set {
  205. if (PanelStyle != null)
  206. PanelStyle.Wrap = value;
  207. else
  208. ViewState ["Wrap"] = value;
  209. }
  210. }
  211. [ThemeableAttribute (false)]
  212. public virtual string DefaultButton {
  213. get {
  214. return ViewState.GetString ("DefaultButton", String.Empty);
  215. }
  216. set {
  217. ViewState ["DefaultButton"] = value;
  218. }
  219. }
  220. public virtual ContentDirection Direction {
  221. get {
  222. if (ControlStyleCreated) {
  223. if (PanelStyle != null)
  224. return PanelStyle.Direction;
  225. else
  226. return ViewState ["Direction"] != null ? (ContentDirection) ViewState ["Direction"] : ContentDirection.NotSet;
  227. }
  228. return ContentDirection.NotSet;
  229. }
  230. set {
  231. if (PanelStyle != null)
  232. PanelStyle.Direction = value;
  233. else
  234. ViewState ["Direction"] = value;
  235. }
  236. }
  237. [LocalizableAttribute (true)]
  238. public virtual string GroupingText {
  239. get {
  240. return ViewState.GetString ("GroupingText", String.Empty);
  241. }
  242. set {
  243. ViewState ["GroupingText"] = value;
  244. }
  245. }
  246. public virtual ScrollBars ScrollBars {
  247. get {
  248. if (ControlStyleCreated) {
  249. if (PanelStyle != null)
  250. return PanelStyle.ScrollBars;
  251. else
  252. return ViewState ["ScrollBars"] != null ? (ScrollBars) ViewState ["Direction"] : ScrollBars.None;
  253. }
  254. return ScrollBars.None;
  255. }
  256. set {
  257. if (PanelStyle != null)
  258. PanelStyle.ScrollBars = value;
  259. else
  260. ViewState ["ScrollBars"] = value;
  261. }
  262. }
  263. protected override Style CreateControlStyle ()
  264. {
  265. return new PanelStyle (ViewState);
  266. }
  267. public override void RenderBeginTag (HtmlTextWriter writer)
  268. {
  269. base.RenderBeginTag (writer);
  270. if (!String.IsNullOrEmpty (GroupingText)) {
  271. writer.RenderBeginTag (HtmlTextWriterTag.Fieldset);
  272. writer.RenderBeginTag (HtmlTextWriterTag.Legend);
  273. writer.Write (GroupingText);
  274. writer.RenderEndTag ();
  275. }
  276. }
  277. public override void RenderEndTag (HtmlTextWriter writer)
  278. {
  279. if (!String.IsNullOrEmpty (GroupingText)) {
  280. writer.RenderEndTag (); // Fieldset
  281. }
  282. base.RenderEndTag (writer);
  283. }
  284. #endif
  285. }
  286. }