为ASP.NET组件加上Collection集合属性(C#)

类别:.NET开发 点击:0 评论:0 推荐:

ASP.NET组件加上Collection集合属性(C#)

 

大家可能都用过Asp.net中的DropDownList控件,该控件有一个Items的集合属性,用于设置组合框的下拉选项。这种体贴的做法让用户倍感温馨,现在,我们也发挥一下自己的的才智,创建一个相同功能的属性。

我们将要创建的控件叫WebPanel,该控件用于网站导航,并且可以收缩,外观如下:

 

第一个图是未收缩的效果,第二个是收缩后的效果,虽然不炫,但是足可以说明问题了。

接下来再看一幅图:
    

这里,就是Collection属性的编辑器,今天我们就围绕这种效果展开话题。

 

在上面的Collection属性的编辑器中,主要分成两个部分,左边的部分是对象的集合,右边部分是选定对象的属性,我们可以对每个属性进行设置赋值。

 

首先,我们定义一个类:StringItem,该类有两个自定义属性:Text用于显示的文本,HyperText是导航的超链接。

using System;

using System.Web.UI;

 

namespace NSWebPanel

{

    /// <summary>

    /// StringItem 的摘要说明。

    /// </summary>

    ///

    public class StringItem : System.Web.UI.Control , IStateManager    

    {

        private string _Text;

        private string _HyperText;

 

        public StringItem()

        {

            //

            // TODO: 在此处添加构造函数逻辑

            //

        }

 

        public string Text

        {

            set

            {

                _Text = value;

            }

            get

            {

                return _Text;

            }

        }

 

        public string HyperText

        {

            set

            {

                _HyperText = value;

            }

            get

            {

                return _HyperText;

            }

        }

        #region IStateManager 成员

 

        void IStateManager.TrackViewState()

        {

            base.TrackViewState();

        }

 

        bool IStateManager.IsTrackingViewState

        {

            get

            {

                return base.IsTrackingViewState;

            }

        }

 

        object IStateManager.SaveViewState()

        {          

            return base.SaveViewState();

        }

 

        void IStateManager.LoadViewState(object state)

        {

            base.LoadViewState(state);

        }

 

        #endregion

    }

}

 

然后,再创建一个用于存放多个StringItem对象的类:StringItems,需要注意的是:该类要继承CollectionBase,这样才能使用默认的Collection属性编辑器。

using System;

using System.Collections;

using System.Web.UI;

 

namespace NSWebPanel

{

    /// <summary>

    /// StringItems 的摘要说明。

    /// </summary>

    public class StringItems : CollectionBase , IStateManager

    {

        private bool marked;

 

        public StringItems() : base()

        {

            //

            // TODO: 在此处添加构造函数逻辑

            //

        }

 

        private void Initialize()

        {

            marked = false;

        }

 

        public StringItem this[int index]

        {

            get

            {

                return (StringItem)base.List[index];

            }

            set

            {

                List[index] = value;

            }

        }

 

        public void Add(StringItem aItem)

        {

            base.List.Add(aItem);

        }

 

        public void Remove(int index)

        {

            if(index < base.Count - 1 && index > 0 )

            {

                base.List.RemoveAt(index);

            }

        }

        #region IStateManager 成员

 

        void IStateManager.TrackViewState()

        {

            for(int i = 0 ; i < base.List.Count; i ++)

            {

                ((IStateManager)base.List[i]).TrackViewState();

            }

        }

 

        bool IStateManager.IsTrackingViewState

        {

            get

            {              

                return marked;

            }

        }

 

        object IStateManager.SaveViewState()

        {      

            object[] iState = new object[base.List.Count];

            for(int i = 0 ; i < base.List.Count; i ++)

            {

                iState[i] = ((IStateManager)base.List[i]).SaveViewState();

            }

            return iState;

        }

 

        void IStateManager.LoadViewState(object state)

        {

            if(state != null)

            {

                object[] viewState = (object[])state;

                for(int i = 0 ; i < viewState.Length ; i ++)

                {

                    ((IStateManager)List[i]).LoadViewState(viewState[i]);

                }

            }

        }

        #endregion

    }

}

最后,新建一个WEB控件库,名称为:NSWebPanel,以下是源代码:

using System;

using System.Web.UI;

using System.Collections;

using System.Web.UI.WebControls;

using System.ComponentModel;

 

namespace NSWebPanel

{

    /// <summary>

    /// WebCustomControl1 的摘要说明。

    /// </summary>

 

    public class WebCustomControl1 : System.Web.UI.WebControls.WebControl,INamingContainer,IStateManager

    {

        private const string SCRIPT = "<table id='t' border='1' width='228' height='145' bordercolor='#000000' cellspacing='0' cellpadding='0' bordercolorlight='#000000' bordercolordark='#FFFFFF'>\n" +

        "<tr>\n" +

        "<td width='228' height='20'>\n" +

        "<table border='0' width='100%' cellpadding='0' cellspacing='0'>\n" +

        "<tr>\n" +

        "<td width='10%' bgcolor='#CCCCFF' id='sign' onmousedown='Shink()' align='center' style='cursor: hand; font-family: Webdings'>5</td>\n" +

        "<td width='90%' style='font-size:14px'>\n" +

             "<p align='center'>{0}</td>\n" +

        "</tr>\n" +

        "</table>\n" +

        "</td>\n" +

        "</tr>\n" +

        "<tr id='downBlock'>\n" +

        "<td width='228' height='113'>\n" +

 

        "<table border='0' cellpadding='0' cellspacing='0' width='100%' height='106' style='font-size:14px'>\n" +

        "<tr>\n" +

        "<td width='100%' align='center' height='21' id='tr1'><a target='_blank' href='{6}'>{1}</a></td>\n" +

        "</tr>\n" +

        "<tr>\n" +

        "<td width='100%' align='center' height='21' id='tr2'><a target='_blank' href='{7}'>{2}</a></td>\n" +

        "</tr>\n" +

        "<tr>\n" +

        "<td width='100%' align='center' height='21' id='tr3'><a target='_blank' href='{8}'>{3}</a></td>\n" +

        "</tr>\n" +

        "<tr>\n" +

        "<td width='100%' align='center' height='21' id='tr4'><a target='_blank' href='{9}'>{4}</a></td>\n" +

        "</tr>\n" +

        "<tr>\n" +

        "<td width='100%' align='center' height='22' id='tr5'><a target='_blank' href='{10}'>{5}</a></td>\n" +

        "</tr>\n" +

        "</table>\n" +

 

        "</td>\n" +

        "</tr>\n" +

        "</table>";

       

        private const string JAVASCRIPT = "<script language='javascript'>\n" +

                                               "var flag = true;\n" +

        "function Shink()\n" +

        "{\n" +

            "if(flag)       \n" +

            "{\n" +

                "document.all.downBlock.style.display = 'none';\n" +

                "document.all.sign.innerText = '6';\n" +

                "document.all.t.height = 20;\n" +

            "}          \n" +

            "else\n" +

            "{\n" +

                "document.all.downBlock.style.display = 'block';\n" +

                "document.all.sign.innerText = '5';\n" +

            "}\n" +

           

            "flag = !flag;\n" +

        "}\n" +

        "</script>\n";

       

 

        private StringItems _Strings;

        //注意下面红色的部分,一定要写哦

        [

            PersistenceMode(PersistenceMode.InnerProperty),

            DesignerSerializationVisibility(DesignerSerializationVisibility.Content)

        ]

        public StringItems Strings

        {          

            get

            {

                if(_Strings == null)

                    _Strings = new StringItems();

                return _Strings;

            }          

 

        }

 

        private string _Caption;

        public string Caption

        {

            get

            {

                return _Caption;

            }

            set

            {

                _Caption = value;

            }

        }

 

        #region 事件

 

        private static object _FieldEvent = null;

        public event System.EventHandler ProcessEvent

        {

            add

            {

                Events.AddHandler(_FieldEvent,value);

            }

            remove

            {

                Events.RemoveHandler(_FieldEvent,value);

            }

        }

 

        #endregion

       

        public void Execute()

        {

            EventHandler hander = (EventHandler)Events[_FieldEvent];

            if(hander != null)

            {

                hander(this,null);

            }

        }

 

        public override void RenderBeginTag(HtmlTextWriter writer)

        {

            base.RenderBeginTag (writer);

            System.Text.StringBuilder str = new System.Text.StringBuilder();

            str.AppendFormat(SCRIPT,this.Caption,_Strings[0].Text,_Strings[1].Text,_Strings[2].Text,_Strings[3].Text,_Strings[4].Text,_Strings[0].HyperText,_Strings[1].HyperText,_Strings[2].HyperText,_Strings[3].HyperText,_Strings[4].HyperText);

            writer.WriteLine(str.ToString());

        }

 

        protected override void OnPreRender(EventArgs e)

        {

            base.OnPreRender (e);

            if(!Page.IsClientScriptBlockRegistered("13C165F4-DB2E-4484-AB1B-5B1F32C8FC8B"))

            {

                Page.RegisterClientScriptBlock("13C165F4-DB2E-4484-AB1B-5B1F32C8FC8B",JAVASCRIPT);

            }

        }

 

 

        public override string ClientID

        {

            get

            {

                return base.ClientID + "LZH";

            }

        }      

 

        protected override void AddAttributesToRender(HtmlTextWriter writer)

        {

            base.AddAttributesToRender (writer);

            writer.AddStyleAttribute("font-size","14px");

        }

 

 

 

        protected override HtmlTextWriterTag TagKey

        {

            get

            {

                return HtmlTextWriterTag.Div;

            }

        }

        #region IStateManager 成员

 

        void IStateManager.TrackViewState()

        {

            base.TrackViewState();

       

        }

 

        bool IStateManager.IsTrackingViewState

        {

            get

            {

                // TODO:  添加 WebCustomControl1.IsTrackingViewState getter 实现

                return false;

            }

        }

 

        object IStateManager.SaveViewState()

        {

            // TODO:  添加 WebCustomControl1.SaveViewState 实现

            return null;

        }

 

        void IStateManager.LoadViewState(object state)

        {

            // TODO:  添加 WebCustomControl1.LoadViewState 实现

        }

 

        #endregion

    }

}

 

本文地址:http://com.8s8s.com/it/it43541.htm