1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok (pbartok@novell.com)
26 using System.Collections.Generic;
27 using System.ComponentModel;
29 using System.Security.Permissions;
31 namespace System.Web.UI.WebControls {
34 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
35 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37 [ParseChildren (true)]
38 [PersistChildrenAttribute (false, false)]
40 public class WebControl : Control, IAttributeAccessor
42 const string DEFAULT_DISABLED_CSS_CLASS = "aspNetDisabled";
44 HtmlTextWriterTag tag;
46 AttributeCollection attributes;
47 StateBag attribute_state;
49 bool track_enabled_state;
52 DisabledCssClass = DEFAULT_DISABLED_CSS_CLASS;
54 public WebControl (HtmlTextWriterTag tag)
60 protected WebControl () : this (HtmlTextWriterTag.Span)
64 protected WebControl (string tag)
66 this.tag = HtmlTextWriterTag.Unknown;
72 [WebSysDescription ("")]
73 [WebCategory ("Behavior")]
74 public virtual string AccessKey {
76 return ViewState.GetString ("AccessKey", string.Empty);
79 if (value == null || value.Length < 2)
80 ViewState ["AccessKey"] = value;
82 throw new ArgumentException ("AccessKey can only be null, empty or a single character", "value");
87 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
88 [WebSysDescription ("")]
89 [WebCategory ("Behavior")]
90 public AttributeCollection Attributes {
92 if (attributes == null) {
93 attribute_state = new StateBag (true);
94 if (IsTrackingViewState)
95 attribute_state.TrackViewState ();
97 attributes = new AttributeCollection (attribute_state);
103 [DefaultValue(typeof (Color), "")]
104 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
105 [WebSysDescription ("")]
106 [WebCategory ("Appearance")]
107 public virtual Color BackColor {
112 return style.BackColor;
115 ControlStyle.BackColor = value;
119 [DefaultValue(typeof (Color), "")]
120 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
121 [WebSysDescription ("")]
122 [WebCategory ("Appearance")]
123 public virtual Color BorderColor {
128 return style.BorderColor;
132 ControlStyle.BorderColor = value;
136 [DefaultValue(BorderStyle.NotSet)]
137 [WebSysDescription ("")]
138 [WebCategory ("Appearance")]
139 public virtual BorderStyle BorderStyle {
142 return BorderStyle.NotSet;
144 return style.BorderStyle;
147 if (value < BorderStyle.NotSet || value > BorderStyle.Outset)
148 throw new ArgumentOutOfRangeException ("value");
150 ControlStyle.BorderStyle = value;
154 [DefaultValue(typeof (Unit), "")]
155 [WebSysDescription ("")]
156 [WebCategory ("Appearance")]
157 public virtual Unit BorderWidth {
162 return style.BorderWidth;
164 set { ControlStyle.BorderWidth = value; }
168 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
169 [WebSysDescription ("")]
170 [WebCategory ("Appearance")]
171 public Style ControlStyle {
174 style = this.CreateControlStyle ();
176 if (IsTrackingViewState)
177 style.TrackViewState ();
185 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
186 [EditorBrowsable (EditorBrowsableState.Never)]
187 public bool ControlStyleCreated {
189 return style != null;
195 [WebSysDescription ("")]
196 [WebCategory ("Appearance")]
197 public virtual string CssClass {
202 return style.CssClass;
205 ControlStyle.CssClass = value;
212 public virtual bool Enabled {
218 if (enabled != value) {
219 if (IsTrackingViewState)
220 track_enabled_state = true;
227 public virtual new bool EnableTheming
229 get { return base.EnableTheming; }
230 set { base.EnableTheming = value; }
233 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
234 [NotifyParentProperty(true)]
235 [WebSysDescription ("")]
236 [WebCategory ("Appearance")]
237 public virtual FontInfo Font {
239 // Oddly enough, it looks like we have to let it create the style
240 // since we can't create a FontInfo without a style owner
241 return ControlStyle.Font;
245 [DefaultValue(typeof (Color), "")]
246 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
247 [WebSysDescription ("")]
248 [WebCategory ("Appearance")]
249 public virtual Color ForeColor {
254 return style.ForeColor;
257 ControlStyle.ForeColor = value;
262 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
267 return (attributes != null && attributes.Count > 0);
271 [DefaultValue(typeof (Unit), "")]
272 [WebSysDescription ("")]
273 [WebCategory ("Layout")]
274 public virtual Unit Height {
282 ControlStyle.Height = value;
287 public virtual new string SkinID
289 get { return base.SkinID; }
290 set { base.SkinID = value; }
294 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
295 [WebSysDescription ("")]
296 [WebCategory ("Style")]
297 public CssStyleCollection Style {
299 return Attributes.CssStyle;
303 [DefaultValue((short)0)]
304 [WebSysDescription ("")]
305 [WebCategory ("Behavior")]
306 public virtual short TabIndex {
308 return ViewState.GetShort ("TabIndex", 0);
311 ViewState ["TabIndex"] = value;
317 [WebSysDescription ("")]
318 [WebCategory ("Behavior")]
319 public virtual string ToolTip {
321 return ViewState.GetString ("ToolTip", string.Empty);
324 ViewState ["ToolTip"] = value;
328 [DefaultValue(typeof (Unit), "")]
329 [WebSysDescription ("")]
330 [WebCategory ("Layout")]
331 public virtual Unit Width {
339 ControlStyle.Width = value;
344 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
345 protected virtual HtmlTextWriterTag TagKey {
352 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
353 protected virtual string TagName {
355 // do this here to avoid potentially costly lookups on every control
356 if (tag_name == null)
357 tag_name = HtmlTextWriter.StaticGetTagName (TagKey);
364 internal bool IsEnabled
367 WebControl wc = this;
371 wc = wc.Parent as WebControl;
376 public static string DisabledCssClass {
382 public virtual bool SupportsDisabledAttribute {
385 public void ApplyStyle (Style s)
387 if (s != null && !s.IsEmpty)
388 ControlStyle.CopyFrom(s);
391 public void CopyBaseAttributes (WebControl controlSrc)
395 if (controlSrc == null)
398 Enabled = controlSrc.Enabled;
400 o = controlSrc.ViewState ["AccessKey"];
402 ViewState ["AccessKey"] = o;
404 o = controlSrc.ViewState ["TabIndex"];
406 ViewState ["TabIndex"] = o;
408 o = controlSrc.ViewState ["ToolTip"];
410 ViewState ["ToolTip"] = o;
412 if (controlSrc.attributes != null) {
413 AttributeCollection attributes = Attributes;
415 foreach (string s in controlSrc.attributes.Keys)
416 attributes [s] = controlSrc.attributes [s];
420 public void MergeStyle (Style s)
422 if (s != null && !s.IsEmpty)
423 ControlStyle.MergeWith(s);
426 public virtual void RenderBeginTag (HtmlTextWriter writer)
428 AddAttributesToRender (writer);
430 if (TagKey == HtmlTextWriterTag.Unknown)
431 writer.RenderBeginTag (TagName);
433 writer.RenderBeginTag (TagKey);
437 public virtual void RenderEndTag (HtmlTextWriter writer)
439 writer.RenderEndTag ();
442 static char[] _script_trim_chars = {';'};
443 internal string BuildScriptAttribute (string name, string tail)
445 AttributeCollection attrs = Attributes;
446 string attr = attrs [name];
448 if (attr == null || attr.Length == 0)
450 if (attr [attr.Length - 1] == ';')
451 attr = attr.TrimEnd (_script_trim_chars);
453 attr = String.Concat (attr, ";", tail);
459 internal void AddDisplayStyleAttribute (HtmlTextWriter writer)
461 if (!ControlStyleCreated)
464 if (!ControlStyle.BorderWidth.IsEmpty ||
465 (ControlStyle.BorderStyle != BorderStyle.None && ControlStyle.BorderStyle != BorderStyle.NotSet) ||
466 !ControlStyle.Height.IsEmpty ||
467 !ControlStyle.Width.IsEmpty)
468 writer.AddStyleAttribute (HtmlTextWriterStyle.Display, "inline-block");
470 void RenderDisabled (HtmlTextWriter writer)
473 if (!SupportsDisabledAttribute)
474 ControlStyle.PrependCssClass (DisabledCssClass);
476 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
481 protected virtual void AddAttributesToRender (HtmlTextWriter writer)
483 RenderDisabled (writer);
485 writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
486 if (AccessKey != string.Empty)
487 writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
489 if (ToolTip != string.Empty)
490 writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
493 writer.AddAttribute (HtmlTextWriterAttribute.Tabindex, TabIndex.ToString ());
495 if (style != null && !style.IsEmpty) {
496 //unbelievable, but see WebControlTest.RenderBeginTag_BorderWidth_xxx
497 if (TagKey == HtmlTextWriterTag.Span)
498 AddDisplayStyleAttribute (writer);
499 style.AddAttributesToRender(writer, this);
502 if (attributes != null)
503 foreach(string s in attributes.Keys)
504 writer.AddAttribute (s, attributes [s]);
507 protected virtual Style CreateControlStyle()
509 return new Style (ViewState);
512 protected override void LoadViewState (object savedState)
514 if (savedState == null || !(savedState is Pair)) {
515 base.LoadViewState (null);
519 Pair pair = (Pair) savedState;
521 base.LoadViewState (pair.First);
522 if (ViewState [System.Web.UI.WebControls.Style.BitStateKey] != null)
523 ControlStyle.LoadBitState ();
525 if (pair.Second != null) {
526 if (attribute_state == null) {
527 attribute_state = new StateBag ();
528 if (IsTrackingViewState)
529 attribute_state.TrackViewState ();
532 attribute_state.LoadViewState (pair.Second);
533 attributes = new AttributeCollection(attribute_state);
536 enabled = ViewState.GetBool ("Enabled", enabled);
538 internal virtual string InlinePropertiesSet ()
540 var properties = new List <string> ();
542 if (BackColor != Color.Empty)
543 properties.Add ("BackColor");
545 if (BorderColor != Color.Empty)
546 properties.Add ("BorderColor");
548 if (BorderStyle != BorderStyle.NotSet)
549 properties.Add ("BorderStyle");
551 if (BorderWidth != Unit.Empty)
552 properties.Add ("BorderWidth");
554 if (CssClass != String.Empty)
555 properties.Add ("CssClass");
557 if (ForeColor != Color.Empty)
558 properties.Add ("ForeColor");
560 if (Height != Unit.Empty)
561 properties.Add ("Height");
563 if (Width != Unit.Empty)
564 properties.Add ("Width");
566 if (properties.Count == 0)
569 return String.Join (", ", properties);
572 internal void VerifyInlinePropertiesNotSet ()
574 var renderOuterTableControl = this as IRenderOuterTable;
575 if (renderOuterTableControl == null || renderOuterTableControl.RenderOuterTable)
578 string properties = InlinePropertiesSet ();
579 if (!String.IsNullOrEmpty (properties)) {
580 bool many = properties.IndexOf (',') > -1;
581 throw new InvalidOperationException (
582 String.Format ("The style propert{0} '{1}' cannot be used while RenderOuterTable is disabled on the {2} control with ID '{3}'",
591 override void Render (HtmlTextWriter writer)
593 if (Adapter != null) {
594 Adapter.Render(writer);
597 RenderBeginTag (writer);
598 RenderContents (writer);
599 RenderEndTag (writer);
603 virtual void RenderContents (HtmlTextWriter writer)
605 base.Render (writer);
608 protected override object SaveViewState ()
610 if (track_enabled_state)
611 ViewState ["Enabled"] = enabled;
614 object attr_view_state = null;
617 style.SaveBitState ();
618 view_state = base.SaveViewState ();
620 if (attribute_state != null)
621 attr_view_state = attribute_state.SaveViewState ();
623 if (view_state == null && attr_view_state == null)
626 return new Pair (view_state, attr_view_state);
629 protected override void TrackViewState()
632 style.TrackViewState ();
634 if (attribute_state != null) {
635 attribute_state.TrackViewState ();
636 attribute_state.SetDirty (true);
639 base.TrackViewState ();
642 string IAttributeAccessor.GetAttribute (string key)
644 if (attributes != null)
645 return attributes [key];
650 void IAttributeAccessor.SetAttribute (string key, string value)
652 Attributes [key] = value;