码迷,mamicode.com
首页 > Web开发 > 详细

关于继承扩展ASP.NET控件(以Textbox为例)

时间:2014-05-19 07:34:53      阅读:426      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   c   

以下是一个相对简陋的扩展, 主要是针对金额显示的Textbox扩展.

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;
using System.Text.RegularExpressions;

[assembly: TagPrefix("XXX.Web.Controls", "XXX")]
namespace XXX.Web.Controls
{
    [ToolboxData("<{0}:DecimalTextbox runat=server></{0}:DecimalTextbox>")]
    [Designer(typeof(XXX.Web.Controls.DecimalTextbox))]
    public class DecimalTextbox : TextBox
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        #region New properties

        private bool _IsRequired = false;

        [TypeConverter(typeof(BooleanConverter))]
        public bool IsRequired
        {
            get
            {
                if (ViewState[this.ID + "_IsRequired"] != null)
                {
                    return (bool)ViewState[this.ID + "_IsRequired"];
                }
                else
                {
                    ViewState[this.ID + "_IsRequired"] = this._IsRequired;
                    return this._IsRequired;
                }
            }
            set
            {
                ViewState[this.ID + "_IsRequired"] = value;
            }
        }

        [TypeConverter(typeof(DecimalConverter))]
        public decimal Value
        {
            get
            {
                Regex reg = new Regex(this.ValidationExpression);
                if (reg.IsMatch(this.RawText))
                {
                    return Convert.ToDecimal(base.Text.Replace(",", "").Replace("$", ""));
                }
                else
                {
                    return 0;
                }
            }
        }

        private string _ValidationExpression = @"^\$?-?[1-9]\d*$|^\$?-?0(\.\d+)$|^\$?-?[1-9]\d*(\.\d+)$|^\$?-?([1-9]{0,3},)(\d{3},)*(\d{3})(\.\d+)?$|^\$?-?0(\.\d+)?$|^\$?-?[1-9]\d{0,2}(\.\d+)?$";

        [TypeConverter(typeof(StringConverter))]
        public string ValidationExpression
        {
            get
            {
                if (ViewState[this.ID + "_ValidationExpression"] != null)
                {
                    return (string)ViewState[this.ID + "_ValidationExpression"];
                }
                else
                {
                    ViewState[this.ID + "_ValidationExpression"] = this._ValidationExpression;
                    return this._ValidationExpression;
                }
            }
            set
            {
                ViewState[this.ID + "_ValidationExpression"] = value;
            }
        }

        private bool _IsNeedThousandSeparator = true;

        [TypeConverter(typeof(BooleanConverter))]
        public bool IsNeedThousandSeparator
        {
            get
            {
                if (ViewState[this.ID + "_IsNeedThousandSeparator"] != null)
                {
                    return (bool)ViewState[this.ID + "_IsNeedThousandSeparator"];
                }
                else
                {
                    ViewState[this.ID + "_IsNeedThousandSeparator"] = this._IsNeedThousandSeparator;
                    return this._IsNeedThousandSeparator;
                }
            }
            set
            {
                ViewState[this.ID + "_IsNeedThousandSeparator"] = value;
            }
        }

        private int _DecimalPlaces = 2;

        [TypeConverter(typeof(Int32Converter))]
        public int DecimalPlaces
        {
            get
            {
                if (ViewState[this.ID + "_DecimalPlaces"] != null)
                {
                    return (int)ViewState[this.ID + "_DecimalPlaces"];
                }
                else
                {
                    ViewState[this.ID + "_DecimalPlaces"] = this._DecimalPlaces;
                    return this._DecimalPlaces;
                }
            }
            set
            {
                ViewState[this.ID + "_DecimalPlaces"] = value;
            }
        }

        private bool _IsNeedDollarSignal = false;

        [TypeConverter(typeof(BooleanConverter))]
        public bool IsNeedDollarSignal
        {
            get
            {
                if (ViewState[this.ID + "_IsNeedDollarSignal"] != null)
                {
                    return (bool)ViewState[this.ID + "_IsNeedDollarSignal"];
                }
                else
                {
                    ViewState[this.ID + "_IsNeedDollarSignal"] = this._IsNeedDollarSignal;
                    return this._IsNeedDollarSignal;
                }
            }
            set
            {
                ViewState[this.ID + "_IsNeedDollarSignal"] = value;
            }
        }

        private string _DollarSignal = "$";

        [TypeConverter(typeof(StringConverter))]
        public string DollarSignal
        {
            get
            {
                if (ViewState[this.ID + "_DollarSignal"] != null)
                {
                    return (string)ViewState[this.ID + "_DollarSignal"];
                }
                else
                {
                    ViewState[this.ID + "_DollarSignal"] = this._DollarSignal;
                    return this._DollarSignal;
                }
            }
            set
            {
                ViewState[this.ID + "_DollarSignal"] = value;
            }
        }

        private string FormatString
        {
            get
            {
                string pre = string.Empty;
                if (this.IsNeedDollarSignal)
                {
                    pre += this.DollarSignal;
                }
                if (this.IsNeedThousandSeparator)
                {
                    pre += "#,###";
                }
                else
                {
                    pre += "#";
                }
                if (this.DecimalPlaces > 0)
                {
                    pre += ".";
                    for (int i = 1; i < this.DecimalPlaces; i++)
                    {
                        pre += "#";
                    }
                    pre += "0";
                }
                return pre;
            }
        }

        private string _RawText = string.Empty;

        public string RawText
        {
            get
            {
                if (ViewState[this.ID + "_RawText"] != null)
                {
                    return (string)ViewState[this.ID + "_RawText"];
                }
                else
                {
                    ViewState[this.ID + "_RawText"] = this._RawText;
                    return this._RawText;
                }
            }
            private set
            {
                ViewState[this.ID + "_RawText"] = value;
            }
        }

        #endregion

        #region Override properties

        public override string SkinID
        {
            get
            {
                if (this.IsRequired)
                {
                    return "DecimalRequired";
                }
                else
                {
                    return "Decimal";
                }
            }
            set
            {
                base.SkinID = value;
            }
        }

        public override string Text
        {
            get
            {
                return ToDecimalFormatString(base.Text);
            }
            set
            {
                this.RawText = value;
                base.Text = ToDecimalString(value);
            }
        }

        #endregion

        #region New functions

        protected string ToDecimalString(string orignal)
        {
            Regex reg = new Regex(this.ValidationExpression);
            if (reg.IsMatch(orignal))
            {
                return orignal.Replace(",", "").Replace("$", "");
            }
            else
            {
                return string.Empty;
            }
        }

        protected string ToDecimalFormatString(string orignal)
        {
            if (!string.IsNullOrEmpty(orignal))
            {
                return this.Value.ToString(FormatString);
            }
            return orignal;
        }

        #endregion
    }
}
View Code

有一下几个自定义属性可以设置:

IsRequired="True" 是否必填, 我这里只是控制SkinID.
IsNeedThousandSeparator="True" 这里是控制是否显示千位符

DecimalPlaces="2" 这里是控制小数位数 

IsNeedDollarSignal="False" 这里是控制是否显示$符号

求助:

ASP.NET自带的控件, 用Toolbox拖入, 某些属性例如"Text"会显示在插入的控件页面代码上

若我现在有一个自定义的属性想在拖入页面的时候, 就在页面代码上就出现(包括默认值). 不知道如何实现, 希望高人指点.

关于继承扩展ASP.NET控件(以Textbox为例),布布扣,bubuko.com

关于继承扩展ASP.NET控件(以Textbox为例)

标签:des   style   blog   class   code   c   

原文地址:http://www.cnblogs.com/xachary/p/3732262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!