Panel.cs 8.7 KB

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