| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577 |
- //
- // System.Web.UI.WebControls.WebControl.cs
- //
- // Authors:
- // Gaurav Vaish ([email protected])
- // Andreas Nahr ([email protected])
- //
- // (C) Gaurav Vaish (2002)
- // (C) 2003 Andreas Nahr
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Web;
- using System.Web.UI;
- using System.Drawing;
- using System.Collections.Specialized;
- namespace System.Web.UI.WebControls
- {
- #if NET_2_0
- [ThemeableAttribute (true)]
- #endif
- [PersistChildrenAttribute(false)]
- [ParseChildrenAttribute(true)]
- public class WebControl : Control, IAttributeAccessor
- {
- HtmlTextWriterTag tagKey;
- AttributeCollection attributes;
- StateBag attributeState;
- Style controlStyle;
- bool enabled = true;
- string tagName;
- protected WebControl () : this (HtmlTextWriterTag.Span)
- {
- }
- public WebControl (HtmlTextWriterTag tag)
- {
- tagKey = tag;
- }
- protected WebControl (string tag)
- {
- tagName = tag;
- }
- #if NET_2_0
- [Localizable (true)]
- #else
- [Bindable (true)]
- #endif
- [DefaultValue (""), WebCategory ("Behavior")]
- [WebSysDescription ("A keyboard shortcut for the WebControl.")]
- public virtual string AccessKey
- {
- get
- {
- object o = ViewState["AccessKey"];
- if(o!=null)
- return (string)o;
- return String.Empty;
- }
- set
- {
- if (value != null && value.Length > 1)
- throw new ArgumentOutOfRangeException ("value");
- ViewState["AccessKey"] = value;
- }
- }
- [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- [WebSysDescription ("Attribute tags for the Webcontrol.")]
- public AttributeCollection Attributes
- {
- get
- {
- if(attributes==null)
- {
- //FIXME: From where to get StateBag and how? I think this method is OK!
- if(attributeState == null)
- {
- attributeState = new StateBag(true);
- if(IsTrackingViewState)
- {
- attributeState.TrackViewState();
- }
- }
- attributes = new AttributeCollection(attributeState);
- }
- return attributes;
- }
- }
- #if !NET_2_0
- [Bindable (true)]
- #endif
- [DefaultValue (typeof(Color), ""), WebCategory ("Appearance")]
- [TypeConverter (typeof (WebColorConverter))]
- [WebSysDescription ("The background color for the WebControl.")]
- public virtual Color BackColor
- {
- get {
- if (!ControlStyleCreated)
- return Color.Empty;
- return ControlStyle.BackColor;
- }
- set {
- ControlStyle.BackColor = value;
- }
- }
- #if !NET_2_0
- [Bindable (true)]
- #endif
- [DefaultValue (typeof(Color), ""), WebCategory ("Appearance")]
- [TypeConverter (typeof (WebColorConverter))]
- [WebSysDescription ("The border color for the WebControl.")]
- public virtual Color BorderColor
- {
- get {
- if (!ControlStyleCreated)
- return Color.Empty;
- return ControlStyle.BorderColor;
- }
- set {
- ControlStyle.BorderColor = value;
- }
- }
- #if !NET_2_0
- [Bindable (true)]
- #endif
- [DefaultValue (typeof(BorderStyle), "NotSet"), WebCategory ("Appearance")]
- [WebSysDescription ("The style/type of the border used for the WebControl.")]
- public virtual BorderStyle BorderStyle
- {
- get {
- if (!ControlStyleCreated)
- return BorderStyle.NotSet;
- return ControlStyle.BorderStyle;
- }
- set {
- ControlStyle.BorderStyle = value;
- }
- }
- #if !NET_2_0
- [Bindable (true)]
- #endif
- [DefaultValue (typeof (Unit), ""), WebCategory ("Appearance")]
- [WebSysDescription ("The width of the border used for the WebControl.")]
- public virtual Unit BorderWidth
- {
- get {
- if (!ControlStyleCreated)
- return Unit.Empty;
- return ControlStyle.BorderWidth;
- }
- set {
- if (value.Value < 0)
- throw new ArgumentOutOfRangeException ("value");
- ControlStyle.BorderWidth = value;
- }
- }
- [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- [WebSysDescription ("The style used to display this Webcontrol.")]
- public Style ControlStyle
- {
- get
- {
- if(controlStyle == null)
- {
- controlStyle = CreateControlStyle();
- if(IsTrackingViewState)
- {
- controlStyle.TrackViewState();
- }
- controlStyle.LoadViewState(null);
- }
- return controlStyle;
- }
- }
- #if NET_2_0
- [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
- #endif
- [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- [WebSysDescription ("Determines if a style exists for this Webcontrol.")]
- public bool ControlStyleCreated
- {
- get
- {
- return (controlStyle!=null);
- }
- }
- #if !NET_2_0
- [Bindable (true)]
- #endif
- [DefaultValue (""), WebCategory ("Appearance")]
- [WebSysDescription ("The cascading stylesheet class that is associated with this WebControl.")]
- public virtual string CssClass
- {
- get
- {
- return ControlStyle.CssClass;
- }
- set
- {
- ControlStyle.CssClass = value;
- }
- }
- #if NET_2_0
- [ThemeableAttribute (false)]
- #endif
- [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
- [WebSysDescription ("The activation state of this WebControl.")]
- public virtual bool Enabled {
- get {
- return enabled;
- }
- set {
- if (enabled != value) {
- ViewState ["Enabled"] = value;
- if (IsTrackingViewState)
- EnableViewState = true;
- }
- enabled = value;
- }
- }
- #if !NET_2_0
- [DefaultValue (null)]
- #endif
- [NotifyParentProperty (true), WebCategory ("Appearance")]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
- [WebSysDescription ("The font of this WebControl.")]
- public virtual FontInfo Font
- {
- get
- {
- return ControlStyle.Font;
- }
- }
- #if !NET_2_0
- [Bindable (true)]
- #endif
- [DefaultValue (typeof(Color), ""), WebCategory ("Appearance")]
- [TypeConverter (typeof (WebColorConverter))]
- [WebSysDescription ("The color that is used to paint the primary display of the WebControl.")]
- public virtual Color ForeColor
- {
- get {
- if (!ControlStyleCreated)
- return Color.Empty;
- return ControlStyle.ForeColor;
- }
- set {
- ControlStyle.ForeColor = value;
- }
- }
- #if !NET_2_0
- [Bindable (true)]
- #endif
- [DefaultValue (typeof(Unit), ""), WebCategory ("Layout")]
- [WebSysDescription ("The height of this WebControl.")]
- public virtual Unit Height
- {
- get
- {
- return ControlStyle.Height;
- }
- set
- {
- if (value.Value < 0)
- throw new ArgumentOutOfRangeException ("value");
- ControlStyle.Height = value;
- }
- }
- [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- [WebSysDescription ("Direct access to the styles used for this Webcontrol.")]
- public CssStyleCollection Style
- {
- get
- {
- return Attributes.CssStyle;
- }
- }
- [DefaultValue (typeof (short), "0"), WebCategory ("Behavior")]
- [WebSysDescription ("The order in which this WebControl gets tabbed through.")]
- public virtual short TabIndex
- {
- get
- {
- object o = ViewState["TabIndex"];
- if(o!=null)
- return (short)o;
- return 0;
- }
- set
- {
- if(value < short.MinValue || value > short.MaxValue)
- throw new ArgumentOutOfRangeException ("value");
- ViewState["TabIndex"] = value;
- }
- }
- #if NET_2_0
- [Localizable (true)]
- #else
- [Bindable (true)]
- #endif
- [DefaultValue (""), WebCategory ("Behavior")]
- [WebSysDescription ("A tooltip that is shown when hovering the mouse above the WebControl.")]
- public virtual string ToolTip
- {
- get
- {
- object o = ViewState["ToolTip"];
- if(o!=null)
- return (string)o;
- return String.Empty;
- }
- set
- {
- ViewState["ToolTip"] = value;
- }
- }
- #if !NET_2_0
- [Bindable (true)]
- #endif
- [DefaultValue ( typeof (Unit), ""), WebCategory ("Layout")]
- [WebSysDescription ("The width of this WebControl.")]
- public virtual Unit Width
- {
- get
- {
- return ControlStyle.Width;
- }
- set
- {
- if (value.Value < 0)
- throw new ArgumentOutOfRangeException ("value");
- ControlStyle.Width = value;
- }
- }
- public void ApplyStyle(Style s)
- {
- if (s != null && !s.IsEmpty)
- ControlStyle.CopyFrom (s);
- }
- public void CopyBaseAttributes(WebControl controlSrc)
- {
- /*
- * AccessKey, Enabled, ToolTip, TabIndex, Attributes
- */
- AccessKey = controlSrc.AccessKey;
- Enabled = controlSrc.Enabled;
- ToolTip = controlSrc.ToolTip;
- TabIndex = controlSrc.TabIndex;
- AttributeCollection otherAtt = controlSrc.Attributes;
- foreach (string key in otherAtt.Keys)
- Attributes [key] = otherAtt [key];
- }
- public void MergeStyle(Style s)
- {
- ControlStyle.MergeWith(s);
- }
- public virtual void RenderBeginTag (HtmlTextWriter writer)
- {
- AddAttributesToRender (writer);
- HtmlTextWriterTag tkey = TagKey;
- // The tagkey goes takes precedence if TagKey != 0 and TagName != null
- if (tkey != 0)
- writer.RenderBeginTag (tkey);
- else
- writer.RenderBeginTag (TagName);
- }
- public virtual void RenderEndTag(HtmlTextWriter writer)
- {
- writer.RenderEndTag();
- }
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- protected virtual HtmlTextWriterTag TagKey
- {
- get
- {
- return tagKey;
- }
- }
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- protected virtual string TagName
- {
- get
- {
- if(tagName == null && TagKey != 0)
- {
- tagName = Enum.Format(typeof(HtmlTextWriterTag), TagKey, "G").ToString();
- }
- // What if tagName is null and tagKey 0?
- // Got the answer: nothing is rendered, empty, null
- return tagName;
- }
- }
- protected virtual void AddAttributesToRender(HtmlTextWriter writer)
- {
- if(ID!=null)
- {
- writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
- }
- if(AccessKey.Length>0)
- {
- writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
- }
- if(!Enabled)
- {
- writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
- }
- if(ToolTip.Length>0)
- {
- writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip);
- }
- if(TabIndex != 0)
- {
- writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString());
- }
- if(ControlStyleCreated)
- {
- if(!ControlStyle.IsEmpty)
- {
- ControlStyle.AddAttributesToRender(writer, this);
- }
- }
- if(attributeState != null){
- IEnumerator ie = Attributes.Keys.GetEnumerator ();
- while (ie.MoveNext ()){
- string key = (string) ie.Current;
- writer.AddAttribute (key, Attributes [key]);
- }
- }
- }
- protected virtual Style CreateControlStyle ()
- {
- return new Style (ViewState);
- }
- protected override void LoadViewState (object savedState)
- {
- if (savedState == null)
- return;
- Pair saved = (Pair) savedState;
- base.LoadViewState (saved.First);
-
- if (ControlStyleCreated || ViewState [System.Web.UI.WebControls.Style.selectionBitString] != null)
- ControlStyle.LoadViewState (null);
- if (saved.Second != null)
- {
- if (attributeState == null)
- {
- attributeState = new StateBag(true);
- attributeState.TrackViewState();
- }
- attributeState.LoadViewState (saved.Second);
- }
-
- object enable = ViewState["Enabled"];
- if (enable!=null)
- {
- Enabled = (bool)enable;
- EnableViewState = true;
- }
- }
- protected override void Render(HtmlTextWriter writer)
- {
- RenderBeginTag (writer);
- RenderContents (writer);
- RenderEndTag (writer);
- }
- protected virtual void RenderContents(HtmlTextWriter writer)
- {
- base.Render (writer);
- }
- protected override object SaveViewState()
- {
- if (EnableViewState)
- ViewState["Enabled"] = enabled;
- if (ControlStyleCreated)
- ControlStyle.SaveViewState ();
-
- object baseView = base.SaveViewState ();
- object attrView = null;
- if (attributeState != null)
- attrView = attributeState.SaveViewState ();
-
- if (baseView == null && attrView == null)
- return null;
- return new Pair (baseView, attrView);
- }
- protected override void TrackViewState()
- {
- base.TrackViewState();
- if (ControlStyleCreated)
- ControlStyle.TrackViewState ();
- if (attributeState != null)
- attributeState.TrackViewState ();
- }
- string IAttributeAccessor.GetAttribute(string key)
- {
- if (attributes != null)
- return Attributes [key] as string;
- return null;
- }
- void IAttributeAccessor.SetAttribute(string key, string value)
- {
- Attributes [key] = value;
- }
- }
- }
|