CheckBoxList 拓展

让保存和反绑定更加简单!





新建 ZCheckBoxList类:

using System.Web.UI.WebControls;
namespace Business
{
    public class ZCheckBoxList : CheckBoxList
    {
        /// 2011-6-19 曾祥展  
        /// 假如勾2,3,4 則SelectedValue="2,3,4"
        /// </remarks>
        public override string SelectedValue
        {
            get
            {
                string _ReturnValue = "";
                this.EnsureChildControls();

                for (int i = 0; i < this.Items.Count; i++)
                {
                    if (this.Items[i].Selected == true)
                    {
                        _ReturnValue += this.Items[i].Value + ",";
                    }
                }
                if (!string.IsNullOrEmpty(_ReturnValue))
                {
                    _ReturnValue = _ReturnValue.TrimEnd(',');
                }
                else
                {
                    return string.Empty;
                }

                return _ReturnValue;
            }

            set
            {
                if (value == null)
                {
                    value = string.Empty;
                }
                string _ReturnValue = "";

                this.EnsureChildControls();
                _ReturnValue = value;


                for (int i = 0; i < this.Items.Count; i++)
                {
                    this.Items[i].Selected = false;
                }

                if (_ReturnValue.Trim().Length != 0)
                {
                    string[] ReturnValue1 = _ReturnValue.Split(',');
                    // 1,2,3
                    for (int intItem = 0; intItem <= ReturnValue1.Length - 1; intItem++)
                    {
                        for (int i = 0; i < this.Items.Count; i++)
                        {
                            if (ReturnValue1[intItem] == this.Items[i].Value)
                            {
                                this.Items[i].Selected = true;
                                break;
                            }
                        }
                    }
                }
            }
        }


    }
}



 

<%@ Register Assembly="Business" Namespace="Business" TagPrefix="cc1" %>

<cc1:ZCheckBoxList ID="cblEnvironment" runat="server"
     RepeatDirection="Horizontal" RepeatColumns="3">
 </cc1:ZCheckBoxList>
 
取值:
this.cblEnvironment.SelectedValue
赋值:
 cblEnvironment.SelectedValue = eid;

posted @ 2011-06-20 21:50  曾祥展  阅读(792)  评论(0编辑  收藏  举报