WebControl

[PersistChildren(false), ParseChildren(true), PermissionSet(SecurityAction.LinkDemand, XML="<PermissionSet class=\"System.Security.PermissionSet\"\r\n               version=\"1\">\r\n   <IPermission class=\"System.Web.AspNetHostingPermission, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\r\n                version=\"1\"\r\n                Level=\"Minimal\"/>\r\n</PermissionSet>\r\n"), PermissionSet(SecurityAction.InheritanceDemand, XML="<PermissionSet class=\"System.Security.PermissionSet\"\r\n               version=\"1\">\r\n   <IPermission class=\"System.Web.AspNetHostingPermission, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"\r\n                version=\"1\"\r\n                Level=\"Minimal\"/>\r\n</PermissionSet>\r\n")]
public class WebControl : Control, IAttributeAccessor
{
    // Fields
    private const int accessKeySet = 8;
    private AttributeCollection attrColl;
    private StateBag attrState;
    private Style controlStyle;
    private const int deferStyleLoadViewState = 1;
    private const int disabled = 2;
    private const int disabledDirty = 4;
    private SimpleBitVector32 flags;
    private const int tabIndexSet = 0x20;
    private HtmlTextWriterTag tagKey;
    private string tagName;
    private const int toolTipSet = 0x10;

    // Methods
    protected WebControl() : this(HtmlTextWriterTag.Span)
    {
    }

    protected WebControl(string tag)
    {
        this.tagKey = HtmlTextWriterTag.Unknown;
        this.tagName = tag;
    }

    public WebControl(HtmlTextWriterTag tag)
    {
        this.tagKey = tag;
    }

    protected virtual void AddAttributesToRender(HtmlTextWriter writer)
    {
        string accessKey;
        if (this.ID != null)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
        }
        if (this.flags[8])
        {
            accessKey = this.AccessKey;
            if (accessKey.Length > 0)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, accessKey);
            }
        }
        if (!this.Enabled)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
        }
        if (this.flags[0x20])
        {
            int tabIndex = this.TabIndex;
            if (tabIndex != 0)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, tabIndex.ToString(NumberFormatInfo.InvariantInfo));
            }
        }
        if (this.flags[0x10])
        {
            accessKey = this.ToolTip;
            if (accessKey.Length > 0)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Title, accessKey);
            }
        }
        if (this.ControlStyleCreated && !this.ControlStyle.IsEmpty)
        {
            this.ControlStyle.AddAttributesToRender(writer, this);
        }
        if (this.attrState != null)
        {
            AttributeCollection attributes = this.Attributes;
            IEnumerator enumerator = attributes.Keys.GetEnumerator();
            while (enumerator.MoveNext())
            {
                string current = (string) enumerator.Current;
                writer.AddAttribute(current, attributes[current]);
            }
        }
    }

    public void ApplyStyle(Style s)
    {
        if ((s != null) && !s.IsEmpty)
        {
            this.ControlStyle.CopyFrom(s);
        }
    }

    public void CopyBaseAttributes(WebControl controlSrc)
    {
        if (controlSrc.flags[8])
        {
            this.AccessKey = controlSrc.AccessKey;
        }
        if (!controlSrc.Enabled)
        {
            this.Enabled = false;
        }
        if (controlSrc.flags[0x10])
        {
            this.ToolTip = controlSrc.ToolTip;
        }
        if (controlSrc.flags[0x20])
        {
            this.TabIndex = controlSrc.TabIndex;
        }
        foreach (string str in controlSrc.Attributes.Keys)
        {
            this.Attributes[str] = controlSrc.Attributes[str];
        }
    }

    protected virtual Style CreateControlStyle()
    {
        return new Style(this.ViewState);
    }

    protected override void LoadViewState(object savedState)
    {
        if (savedState != null)
        {
            Pair pair = (Pair) savedState;
            base.LoadViewState(pair.First);
            if (this.ControlStyleCreated || (this.ViewState["_!SB"] != null))
            {
                this.ControlStyle.LoadViewState(null);
            }
            else
            {
                this.flags[1] = true;
            }
            if (pair.Second != null)
            {
                if (this.attrState == null)
                {
                    this.attrState = new StateBag(true);
                    this.attrState.TrackViewState();
                }
                this.attrState.LoadViewState(pair.Second);
            }
        }
        object obj2 = this.ViewState["Enabled"];
        if (obj2 != null)
        {
            this.flags[2] = !((bool) obj2);
            this.flags[4] = true;
        }
        if (((IDictionary) this.ViewState).Contains("AccessKey"))
        {
            this.flags[8] = true;
        }
        if (((IDictionary) this.ViewState).Contains("TabIndex"))
        {
            this.flags[0x20] = true;
        }
        if (((IDictionary) this.ViewState).Contains("ToolTip"))
        {
            this.flags[0x10] = true;
        }
    }

    public void MergeStyle(Style s)
    {
        if ((s != null) && !s.IsEmpty)
        {
            this.ControlStyle.MergeWith(s);
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        this.RenderBeginTag(writer);
        this.RenderContents(writer);
        this.RenderEndTag(writer);
    }

    public virtual void RenderBeginTag(HtmlTextWriter writer)
    {
        this.AddAttributesToRender(writer);
        HtmlTextWriterTag tagKey = this.TagKey;
        if (tagKey != HtmlTextWriterTag.Unknown)
        {
            writer.RenderBeginTag(tagKey);
        }
        else
        {
            writer.RenderBeginTag(this.TagName);
        }
    }

    protected virtual void RenderContents(HtmlTextWriter writer)
    {
        base.Render(writer);
    }

    public virtual void RenderEndTag(HtmlTextWriter writer)
    {
        writer.RenderEndTag();
    }

    protected override object SaveViewState()
    {
        Pair pair = null;
        if (this.flags[4])
        {
            this.ViewState["Enabled"] = !this.flags[2];
        }
        if (this.ControlStyleCreated)
        {
            this.ControlStyle.SaveViewState();
        }
        object x = base.SaveViewState();
        object y = null;
        if (this.attrState != null)
        {
            y = this.attrState.SaveViewState();
        }
        if ((x == null) && (y == null))
        {
            return pair;
        }
        return new Pair(x, y);
    }

    string IAttributeAccessor.GetAttribute(string name)
    {
        if (this.attrState == null)
        {
            return null;
        }
        return (string) this.attrState[name];
    }

    void IAttributeAccessor.SetAttribute(string name, string value)
    {
        this.Attributes[name] = value;
    }

    protected override void TrackViewState()
    {
        base.TrackViewState();
        if (this.ControlStyleCreated)
        {
            this.ControlStyle.TrackViewState();
        }
        if (this.attrState != null)
        {
            this.attrState.TrackViewState();
        }
    }

    // Properties
    [WebSysDescription("WebControl_AccessKey"), DefaultValue(""), Bindable(true), WebCategory("Behavior")]
    public virtual string AccessKey
    {
        get
        {
            if (this.flags[8])
            {
                string str = (string) this.ViewState["AccessKey"];
                if (str != null)
                {
                    return str;
                }
            }
            return string.Empty;
        }
        set
        {
            if ((value != null) && (value.Length > 1))
            {
                throw new ArgumentOutOfRangeException("value");
            }
            this.ViewState["AccessKey"] = value;
            this.flags[8] = true;
        }
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), WebSysDescription("WebControl_Attributes"), Browsable(false)]
    public AttributeCollection Attributes
    {
        get
        {
            if (this.attrColl == null)
            {
                if (this.attrState == null)
                {
                    this.attrState = new StateBag(true);
                    if (base.IsTrackingViewState)
                    {
                        this.attrState.TrackViewState();
                    }
                }
                this.attrColl = new AttributeCollection(this.attrState);
            }
            return this.attrColl;
        }
    }

    [WebCategory("Appearance"), DefaultValue(typeof(Color), ""), WebSysDescription("WebControl_BackColor"), TypeConverter(typeof(WebColorConverter)), Bindable(true)]
    public virtual Color BackColor
    {
        get
        {
            if (!this.ControlStyleCreated)
            {
                return Color.Empty;
            }
            return this.ControlStyle.BackColor;
        }
        set
        {
            this.ControlStyle.BackColor = value;
        }
    }

    [Bindable(true), WebCategory("Appearance"), WebSysDescription("WebControl_BorderColor"), TypeConverter(typeof(WebColorConverter)), DefaultValue(typeof(Color), "")]
    public virtual Color BorderColor
    {
        get
        {
            if (!this.ControlStyleCreated)
            {
                return Color.Empty;
            }
            return this.ControlStyle.BorderColor;
        }
        set
        {
            this.ControlStyle.BorderColor = value;
        }
    }

    [DefaultValue(0), Bindable(true), WebCategory("Appearance"), WebSysDescription("WebControl_BorderStyle")]
    public virtual BorderStyle BorderStyle
    {
        get
        {
            if (!this.ControlStyleCreated)
            {
                return BorderStyle.NotSet;
            }
            return this.ControlStyle.BorderStyle;
        }
        set
        {
            this.ControlStyle.BorderStyle = value;
        }
    }

    [Bindable(true), DefaultValue(typeof(Unit), ""), WebSysDescription("WebControl_BorderWidth"), WebCategory("Appearance")]
    public virtual Unit BorderWidth
    {
        get
        {
            if (!this.ControlStyleCreated)
            {
                return Unit.Empty;
            }
            return this.ControlStyle.BorderWidth;
        }
        set
        {
            if (value.Value < 0.0)
            {
                throw new ArgumentOutOfRangeException("value");
            }
            this.ControlStyle.BorderWidth = value;
        }
    }

    [WebSysDescription("WebControl_ControlStyle"), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public Style ControlStyle
    {
        get
        {
            if (this.controlStyle == null)
            {
                this.controlStyle = this.CreateControlStyle();
                if (base.IsTrackingViewState)
                {
                    this.controlStyle.TrackViewState();
                }
                if (this.flags[1])
                {
                    this.flags[1] = false;
                    this.controlStyle.LoadViewState(null);
                }
            }
            return this.controlStyle;
        }
    }

    [Browsable(false), WebSysDescription("WebControl_ControlStyleCreated"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public bool ControlStyleCreated
    {
        get
        {
            return (this.controlStyle != null);
        }
    }

    [WebCategory("Appearance"), DefaultValue(""), WebSysDescription("WebControl_CSSClassName"), Bindable(true)]
    public virtual string CssClass
    {
        get
        {
            if (!this.ControlStyleCreated)
            {
                return string.Empty;
            }
            return this.ControlStyle.CssClass;
        }
        set
        {
            this.ControlStyle.CssClass = value;
        }
    }

    [WebCategory("Behavior"), DefaultValue(true), WebSysDescription("WebControl_Enabled"), Bindable(true)]
    public virtual bool Enabled
    {
        get
        {
            return !this.flags[2];
        }
        set
        {
            bool flag = !this.flags[2];
            if (flag != value)
            {
                this.flags[2] = !value;
                if (base.IsTrackingViewState)
                {
                    this.flags[4] = true;
                }
            }
        }
    }

    [NotifyParentProperty(true), DefaultValue((string) null), WebSysDescription("WebControl_Font"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), WebCategory("Appearance")]
    public virtual FontInfo Font
    {
        get
        {
            return this.ControlStyle.Font;
        }
    }

    [WebSysDescription("WebControl_ForeColor"), TypeConverter(typeof(WebColorConverter)), Bindable(true), WebCategory("Appearance"), DefaultValue(typeof(Color), "")]
    public virtual Color ForeColor
    {
        get
        {
            if (!this.ControlStyleCreated)
            {
                return Color.Empty;
            }
            return this.ControlStyle.ForeColor;
        }
        set
        {
            this.ControlStyle.ForeColor = value;
        }
    }

    internal bool HasAttributes
    {
        get
        {
            return (((this.attrColl != null) && (this.attrColl.Count > 0)) || ((this.attrState != null) && (this.attrState.Count > 0)));
        }
    }

    [WebCategory("Layout"), WebSysDescription("WebControl_Height"), DefaultValue(typeof(Unit), ""), Bindable(true)]
    public virtual Unit Height
    {
        get
        {
            if (!this.ControlStyleCreated)
            {
                return Unit.Empty;
            }
            return this.ControlStyle.Height;
        }
        set
        {
            if (value.Value < 0.0)
            {
                throw new ArgumentOutOfRangeException("value");
            }
            this.ControlStyle.Height = value;
        }
    }

    [WebSysDescription("WebControl_Style"), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public CssStyleCollection Style
    {
        get
        {
            return this.Attributes.CssStyle;
        }
    }

    [WebCategory("Behavior"), DefaultValue((short) 0), WebSysDescription("WebControl_TabIndex")]
    public virtual short TabIndex
    {
        get
        {
            if (this.flags[0x20])
            {
                object obj2 = this.ViewState["TabIndex"];
                if (obj2 != null)
                {
                    return (short) obj2;
                }
            }
            return 0;
        }
        set
        {
            this.ViewState["TabIndex"] = value;
            this.flags[0x20] = true;
        }
    }

    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    protected virtual HtmlTextWriterTag TagKey
    {
        get
        {
            return this.tagKey;
        }
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
    protected virtual string TagName
    {
        get
        {
            if ((this.tagName == null) && (this.tagKey != HtmlTextWriterTag.Unknown))
            {
                this.tagName = Enum.Format(typeof(HtmlTextWriterTag), this.tagKey, "G").ToLower(CultureInfo.InvariantCulture);
            }
            return this.tagName;
        }
    }

    [Bindable(true), DefaultValue(""), WebSysDescription("WebControl_Tooltip"), WebCategory("Behavior")]
    public virtual string ToolTip
    {
        get
        {
            if (this.flags[0x10])
            {
                string str = (string) this.ViewState["ToolTip"];
                if (str != null)
                {
                    return str;
                }
            }
            return string.Empty;
        }
        set
        {
            this.ViewState["ToolTip"] = value;
            this.flags[0x10] = true;
        }
    }

    [DefaultValue(typeof(Unit), ""), WebSysDescription("WebControl_Width"), Bindable(true), WebCategory("Layout")]
    public virtual Unit Width
    {
        get
        {
            if (!this.ControlStyleCreated)
            {
                return Unit.Empty;
            }
            return this.ControlStyle.Width;
        }
        set
        {
            if (value.Value < 0.0)
            {
                throw new ArgumentOutOfRangeException("value");
            }
            this.ControlStyle.Width = value;
        }
    }
}

posted on 2010-02-21 16:20  木可  阅读(556)  评论(0)    收藏  举报

导航