自定义控件,自定义视图状态,复杂属性

来源:互联网 发布:jquery.modal.js下载 编辑:程序博客网 时间:2024/05/19 20:42

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections;
namespace myWebControls.Menu_v2
{
    [ParseChildren(true)]
    public class Menu_v2 : Panel
    {
        [TypeConverter(typeof(StringArrayConverter))]
        public string[] AAA
        {
            get
            {
                object obj = ViewState["AAA"];
                return obj == null ? null : (string[])obj;
            }
            set { ViewState["AAA"] = value; }
        }

        BackStyle backstyle;

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public BackStyle BBB
        {
            get {
                if (backstyle == null)
                {
                    backstyle = new BackStyle();
                    if (IsTrackingViewState)
                        ((IStateManager)backstyle).TrackViewState();
                   
                }
                return backstyle;
            }
        }

        protected override void LoadViewState(object savedState)
        {
            Pair p = savedState as Pair;
            if (p != null)
            {
                base.LoadViewState(p.First);
                ((IStateManager)BBB).LoadViewState(p.Second);
                return;
            }
            base.LoadViewState(savedState);
        }

        protected override object SaveViewState()
        {
            object baseState = base.SaveViewState();
            object thisState = null;

            if (backstyle != null)
                thisState = ((IStateManager)backstyle).SaveViewState();

            if (thisState != null)
                return new Pair(baseState, thisState);
            else
                return baseState;
        }

        protected override void TrackViewState()
        {
            if (backstyle != null)
                ((IStateManager)backstyle).TrackViewState();

            base.TrackViewState();
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            base.RenderControl(writer);
           
            writer.Write("ABCDEFG");
            writer.Write(BBB.BackColor.ToString());
        }
       
    }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class BackStyle:IStateManager
    {
        public BackStyle(){}

        [NotifyParentProperty(true)]
        public System.Drawing.Color BackColor
        {
            get { return ViewState["BackColor"]==null ? System.Drawing.Color.Black:(System.Drawing.Color)ViewState["BackColor"]; }
            set { ViewState["BackColor"] = value; }
        }

        private bool _isTrackingViewState;
        private StateBag _viewState;

        protected StateBag ViewState
        {
            get
            {
                if (_viewState == null)
                {
                    _viewState = new StateBag(false);
                    if (_isTrackingViewState) ((IStateManager)_viewState).TrackViewState();
                }
                return _viewState;
            }
        }

        #region IStateManager 成员
        [Browsable(false)]
        public bool IsTrackingViewState
        {
            get { return _isTrackingViewState; }
        }

        public void LoadViewState(object state)
        {
            if (state != null)
                ((IStateManager)ViewState).LoadViewState(state);
        }

        public object SaveViewState()
        {
            object savedState = null;
            if (_viewState != null)
                savedState =((IStateManager)_viewState).SaveViewState();
            return savedState;
        }

        public void TrackViewState()
        {
            _isTrackingViewState = true;
            if (_viewState != null)
                ((IStateManager)_viewState).TrackViewState();
        }

        internal void SetDirty()
        {
            _viewState.SetDirty(true);
        }

        #endregion
    }
}