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)
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)]
39 [PersistChildrenAttribute (false, false)]
42 [PersistChildrenAttribute (false)]
44 public class WebControl : Control, IAttributeAccessor {
46 HtmlTextWriterTag tag;
48 AttributeCollection attributes;
49 StateBag attribute_state;
51 bool track_enabled_state;
53 public WebControl (HtmlTextWriterTag tag)
59 protected WebControl () : this (HtmlTextWriterTag.Span)
63 protected WebControl (string tag)
65 this.tag = HtmlTextWriterTag.Unknown;
74 [WebSysDescription ("")]
75 [WebCategory ("Behavior")]
76 public virtual string AccessKey {
78 return ViewState.GetString ("AccessKey", string.Empty);
81 if (value == null || value.Length < 2)
82 ViewState ["AccessKey"] = value;
84 throw new ArgumentException ("AccessKey can only be null, empty or a single character", "value");
89 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
90 [WebSysDescription ("")]
91 [WebCategory ("Behavior")]
92 public AttributeCollection Attributes {
94 if (attributes == null) {
95 attribute_state = new StateBag (true);
96 if (IsTrackingViewState)
97 attribute_state.TrackViewState ();
99 attributes = new AttributeCollection (attribute_state);
108 [DefaultValue(typeof (Color), "")]
109 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
110 [WebSysDescription ("")]
111 [WebCategory ("Appearance")]
112 public virtual Color BackColor {
117 return style.BackColor;
120 ControlStyle.BackColor = value;
127 [DefaultValue(typeof (Color), "")]
128 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
129 [WebSysDescription ("")]
130 [WebCategory ("Appearance")]
131 public virtual Color BorderColor {
136 return style.BorderColor;
140 ControlStyle.BorderColor = value;
147 [DefaultValue(BorderStyle.NotSet)]
148 [WebSysDescription ("")]
149 [WebCategory ("Appearance")]
150 public virtual BorderStyle BorderStyle {
153 return BorderStyle.NotSet;
155 return style.BorderStyle;
158 if (value < BorderStyle.NotSet || value > BorderStyle.Outset)
159 throw new ArgumentOutOfRangeException ("value");
161 ControlStyle.BorderStyle = value;
168 [DefaultValue(typeof (Unit), "")]
169 [WebSysDescription ("")]
170 [WebCategory ("Appearance")]
171 public virtual Unit BorderWidth {
176 return style.BorderWidth;
179 ControlStyle.BorderWidth = value;
184 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
185 [WebSysDescription ("")]
186 [WebCategory ("Appearance")]
187 public Style ControlStyle {
190 style = this.CreateControlStyle ();
192 if (IsTrackingViewState)
193 style.TrackViewState ();
201 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
203 [EditorBrowsable (EditorBrowsableState.Never)]
205 public bool ControlStyleCreated {
207 return style != null;
215 [WebSysDescription ("")]
216 [WebCategory ("Appearance")]
217 public virtual string CssClass {
222 return style.CssClass;
225 ControlStyle.CssClass = value;
234 public virtual bool Enabled {
240 if (enabled != value) {
241 if (IsTrackingViewState)
242 track_enabled_state = true;
250 public virtual new bool EnableTheming
252 get { return base.EnableTheming; }
253 set { base.EnableTheming = value; }
260 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
261 [NotifyParentProperty(true)]
262 [WebSysDescription ("")]
263 [WebCategory ("Appearance")]
264 public virtual FontInfo Font {
266 // Oddly enough, it looks like we have to let it create the style
267 // since we can't create a FontInfo without a style owner
268 return ControlStyle.Font;
275 [DefaultValue(typeof (Color), "")]
276 [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
277 [WebSysDescription ("")]
278 [WebCategory ("Appearance")]
279 public virtual Color ForeColor {
284 return style.ForeColor;
287 ControlStyle.ForeColor = value;
293 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
294 public bool HasAttributes
297 return (attributes != null && attributes.Count > 0);
305 [DefaultValue(typeof (Unit), "")]
306 [WebSysDescription ("")]
307 [WebCategory ("Layout")]
308 public virtual Unit Height {
316 ControlStyle.Height = value;
322 public virtual new string SkinID
324 get { return base.SkinID; }
325 set { base.SkinID = value; }
330 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
331 [WebSysDescription ("")]
332 [WebCategory ("Style")]
333 public CssStyleCollection Style {
335 return Attributes.CssStyle;
339 [DefaultValue((short)0)]
340 [WebSysDescription ("")]
341 [WebCategory ("Behavior")]
342 public virtual short TabIndex {
344 return ViewState.GetShort ("TabIndex", 0);
347 ViewState ["TabIndex"] = value;
358 [WebSysDescription ("")]
359 [WebCategory ("Behavior")]
360 public virtual string ToolTip {
362 return ViewState.GetString ("ToolTip", string.Empty);
365 ViewState ["ToolTip"] = value;
372 [DefaultValue(typeof (Unit), "")]
373 [WebSysDescription ("")]
374 [WebCategory ("Layout")]
375 public virtual Unit Width {
383 ControlStyle.Width = value;
388 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
389 protected virtual HtmlTextWriterTag TagKey {
396 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
397 protected virtual string TagName {
399 // do this here to avoid potentially costly lookups on every control
400 if (tag_name == null)
401 tag_name = HtmlTextWriter.StaticGetTagName (TagKey);
408 protected internal bool IsEnabled
415 for (int i = 0; i < Controls.Count; i++) {
416 WebControl wc = Controls [i] as WebControl;
417 if (wc != null && wc.IsEnabled)
426 public void ApplyStyle (Style s)
428 if (s != null && !s.IsEmpty)
429 ControlStyle.CopyFrom(s);
432 public void CopyBaseAttributes (WebControl controlSrc)
436 if (controlSrc == null)
439 Enabled = controlSrc.Enabled;
441 o = controlSrc.ViewState ["AccessKey"];
443 ViewState ["AccessKey"] = o;
445 o = controlSrc.ViewState ["TabIndex"];
447 ViewState ["TabIndex"] = o;
449 o = controlSrc.ViewState ["ToolTip"];
451 ViewState ["ToolTip"] = o;
453 if (controlSrc.attributes != null)
454 foreach (string s in controlSrc.attributes.Keys)
455 Attributes [s] = controlSrc.attributes [s];
458 public void MergeStyle (Style s)
460 if (s != null && !s.IsEmpty)
461 ControlStyle.MergeWith(s);
464 public virtual void RenderBeginTag (HtmlTextWriter writer)
466 AddAttributesToRender (writer);
468 if (TagKey == HtmlTextWriterTag.Unknown)
469 writer.RenderBeginTag (TagName);
471 writer.RenderBeginTag (TagKey);
475 public virtual void RenderEndTag (HtmlTextWriter writer)
477 writer.RenderEndTag ();
480 static char[] _script_trim_chars = {';'};
481 internal string BuildScriptAttribute (string name, string tail)
483 AttributeCollection attrs = Attributes;
484 string attr = attrs [name];
486 if (attr == null || attr.Length == 0)
488 if (attr [attr.Length - 1] == ';')
489 attr = attr.TrimEnd (_script_trim_chars);
491 attr = String.Concat (attr, ";", tail);
498 internal void AddDisplayStyleAttribute (HtmlTextWriter writer)
500 if (!ControlStyleCreated)
503 if (!ControlStyle.BorderWidth.IsEmpty ||
504 (ControlStyle.BorderStyle != BorderStyle.None && ControlStyle.BorderStyle != BorderStyle.NotSet) ||
505 !ControlStyle.Height.IsEmpty ||
506 !ControlStyle.Width.IsEmpty)
507 writer.AddStyleAttribute (HtmlTextWriterStyle.Display, "inline-block");
511 protected virtual void AddAttributesToRender (HtmlTextWriter writer)
514 writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
516 if (AccessKey != string.Empty)
517 writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
520 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled", false);
522 if (ToolTip != string.Empty)
523 writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
526 writer.AddAttribute (HtmlTextWriterAttribute.Tabindex, TabIndex.ToString ());
528 if (style != null && !style.IsEmpty) {
530 //unbelievable, but see WebControlTest.RenderBeginTag_BorderWidth_xxx
531 if (TagKey == HtmlTextWriterTag.Span)
532 AddDisplayStyleAttribute (writer);
534 style.AddAttributesToRender(writer, this);
537 if (attributes != null)
538 foreach(string s in attributes.Keys)
539 writer.AddAttribute (s, attributes [s]);
542 protected virtual Style CreateControlStyle()
544 return new Style (ViewState);
547 protected override void LoadViewState (object savedState)
549 if (savedState == null) {
550 base.LoadViewState (null);
554 Pair pair = (Pair) savedState;
556 base.LoadViewState (pair.First);
557 if (ViewState [System.Web.UI.WebControls.Style.BitStateKey] != null)
558 ControlStyle.LoadBitState ();
560 if (pair.Second != null) {
561 if (attribute_state == null) {
562 attribute_state = new StateBag ();
563 if (IsTrackingViewState)
564 attribute_state.TrackViewState ();
566 attribute_state.LoadViewState (pair.Second);
567 attributes = new AttributeCollection(attribute_state);
570 enabled = ViewState.GetBool ("Enabled", enabled);
578 override void Render (HtmlTextWriter writer)
580 RenderBeginTag (writer);
581 RenderContents (writer);
582 RenderEndTag (writer);
590 virtual void RenderContents (HtmlTextWriter writer)
592 base.Render (writer);
595 protected override object SaveViewState ()
597 if (track_enabled_state)
598 ViewState ["Enabled"] = enabled;
601 object attr_view_state = null;
604 style.SaveBitState ();
605 view_state = base.SaveViewState ();
607 if (attribute_state != null)
608 attr_view_state = attribute_state.SaveViewState ();
610 if (view_state == null && attr_view_state == null)
613 return new Pair (view_state, attr_view_state);
616 protected override void TrackViewState()
619 style.TrackViewState ();
621 if (attribute_state != null)
622 attribute_state.TrackViewState ();
624 base.TrackViewState ();
627 string IAttributeAccessor.GetAttribute (string key)
629 if (attributes != null)
630 return attributes [key];
635 void IAttributeAccessor.SetAttribute (string key, string value)
637 Attributes [key] = value;