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)]
38 [PersistChildrenAttribute (false, false)]
39 [ParseChildrenAttribute (true, ChildControlType = typeof(Control))]
42 [PersistChildrenAttribute (false)]
43 [ParseChildrenAttribute (true)]
45 public class WebControl : Control, IAttributeAccessor {
47 HtmlTextWriterTag tag;
49 AttributeCollection attributes;
50 StateBag attribute_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 ViewState ["Enabled"] = value;
250 public virtual new bool EnableTheming
252 get { return base.EnableTheming; }
253 set { 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)]
295 public bool HasAttributes
298 throw new NotImplementedException ();
306 [DefaultValue(typeof (Unit), "")]
307 [WebSysDescription ("")]
308 [WebCategory ("Layout")]
309 public virtual Unit Height {
317 ControlStyle.Height = value;
324 public virtual new string SkinID
326 get { return base.SkinID; }
327 set { base.SkinID = value; }
332 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
333 [WebSysDescription ("")]
334 [WebCategory ("Style")]
335 public CssStyleCollection Style {
337 return Attributes.CssStyle;
341 [DefaultValue((short)0)]
342 [WebSysDescription ("")]
343 [WebCategory ("Behavior")]
344 public virtual short TabIndex {
346 return ViewState.GetShort ("TabIndex", 0);
349 ViewState ["TabIndex"] = value;
360 [WebSysDescription ("")]
361 [WebCategory ("Behavior")]
362 public virtual string ToolTip {
364 return ViewState.GetString ("ToolTip", string.Empty);
367 ViewState ["ToolTip"] = value;
374 [DefaultValue(typeof (Unit), "")]
375 [WebSysDescription ("")]
376 [WebCategory ("Layout")]
377 public virtual Unit Width {
385 ControlStyle.Width = value;
390 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
391 protected virtual HtmlTextWriterTag TagKey {
398 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
399 protected virtual string TagName {
401 // do this here to avoid potentially costly lookups on every control
402 if (tag_name == null)
403 tag_name = HtmlTextWriter.StaticGetTagName (TagKey);
411 protected internal bool IsEnabled
414 throw new NotImplementedException ();
420 public void ApplyStyle (Style s)
422 if (s != null && !s.IsEmpty)
423 ControlStyle.CopyFrom(s);
426 public void CopyBaseAttributes (WebControl controlSrc)
430 if (controlSrc == null)
433 o = controlSrc.ViewState ["Enabled"];
438 o = controlSrc.ViewState ["AccessKey"];
440 ViewState ["AccessKey"] = o;
442 o = controlSrc.ViewState ["TabIndex"];
444 ViewState ["TabIndex"] = o;
446 o = controlSrc.ViewState ["ToolTip"];
448 ViewState ["ToolTip"] = o;
450 if (controlSrc.attributes != null)
451 foreach (string s in controlSrc.attributes.Keys)
452 Attributes [s] = controlSrc.attributes [s];
455 public void MergeStyle (Style s)
457 if (s != null && !s.IsEmpty)
458 ControlStyle.MergeWith(s);
461 public virtual void RenderBeginTag (HtmlTextWriter writer)
463 AddAttributesToRender (writer);
465 if (TagKey == HtmlTextWriterTag.Unknown)
466 writer.RenderBeginTag (TagName);
468 writer.RenderBeginTag (TagKey);
472 public virtual void RenderEndTag (HtmlTextWriter writer)
474 writer.RenderEndTag ();
477 protected virtual void AddAttributesToRender (HtmlTextWriter writer)
480 writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
482 if (AccessKey != string.Empty)
483 writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
486 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
488 if (ToolTip != string.Empty)
489 writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
492 writer.AddAttribute (HtmlTextWriterAttribute.Tabindex, TabIndex.ToString ());
494 if (style != null && !style.IsEmpty)
495 style.AddAttributesToRender(writer, this);
497 if (attributes != null)
498 foreach(string s in attributes.Keys)
499 writer.AddAttribute (s, attributes [s]);
502 protected virtual Style CreateControlStyle()
504 style = new Style (ViewState);
508 protected override void LoadViewState (object savedState)
510 if (savedState == null) {
511 base.LoadViewState(null);
515 Triplet triplet = (Triplet) savedState;
517 base.LoadViewState (triplet.First);
519 if (triplet.Second != null) {
520 if (attribute_state == null) {
521 attribute_state = new StateBag ();
522 if (IsTrackingViewState)
523 attribute_state.TrackViewState ();
525 attribute_state.LoadViewState (triplet.Second);
526 attributes = new AttributeCollection(attribute_state);
529 if (triplet.Third != null) {
531 style = CreateControlStyle ();
533 style.LoadViewState (triplet.Third);
536 enabled = ViewState.GetBool("Enabled", true);
544 override void Render (HtmlTextWriter writer)
546 RenderBeginTag (writer);
547 RenderContents (writer);
548 RenderEndTag (writer);
556 virtual void RenderContents (HtmlTextWriter writer)
558 base.Render (writer);
561 protected override object SaveViewState ()
564 object attr_view_state = null;
565 object style_view_state = null;
567 view_state = base.SaveViewState ();
569 if (attribute_state != null)
570 attr_view_state = attribute_state.SaveViewState ();
573 style_view_state = style.SaveViewState ();
575 if (view_state == null && attr_view_state == null && style_view_state == null)
578 return new Triplet (view_state, attr_view_state, style_view_state);
581 protected override void TrackViewState()
584 style.TrackViewState ();
586 if (attribute_state != null)
587 attribute_state.TrackViewState ();
589 base.TrackViewState ();
592 string IAttributeAccessor.GetAttribute (string key)
594 if (attributes != null)
595 return attributes [key];
600 void IAttributeAccessor.SetAttribute (string key, string value)
602 Attributes [key] = value;