关于继承扩展ASP.NET控件(以Textbox为例)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了关于继承扩展ASP.NET控件(以Textbox为例),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5150字,纯文字阅读大概需要8分钟。
内容图文
以下是一个相对简陋的扩展, 主要是针对金额显示的Textbox扩展.
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))] publicclass DecimalTextbox : TextBox { protectedoverridevoid OnInit(EventArgs e) { base.OnInit(e); } #region New properties privatebool _IsRequired = false; [TypeConverter(typeof(BooleanConverter))] publicbool IsRequired { get { if (ViewState[this.ID + "_IsRequired"] != null) { return (bool)ViewState[this.ID + "_IsRequired"]; } else { ViewState[this.ID + "_IsRequired"] = this._IsRequired; returnthis._IsRequired; } } set { ViewState[this.ID + "_IsRequired"] = value; } } [TypeConverter(typeof(DecimalConverter))] publicdecimal Value { get { Regex reg = new Regex(this.ValidationExpression); if (reg.IsMatch(this.RawText)) { return Convert.ToDecimal(base.Text.Replace(",", "").Replace("$", "")); } else { return0; } } } privatestring _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))] publicstring ValidationExpression { get { if (ViewState[this.ID + "_ValidationExpression"] != null) { return (string)ViewState[this.ID + "_ValidationExpression"]; } else { ViewState[this.ID + "_ValidationExpression"] = this._ValidationExpression; returnthis._ValidationExpression; } } set { ViewState[this.ID + "_ValidationExpression"] = value; } } privatebool _IsNeedThousandSeparator = true; [TypeConverter(typeof(BooleanConverter))] publicbool IsNeedThousandSeparator { get { if (ViewState[this.ID + "_IsNeedThousandSeparator"] != null) { return (bool)ViewState[this.ID + "_IsNeedThousandSeparator"]; } else { ViewState[this.ID + "_IsNeedThousandSeparator"] = this._IsNeedThousandSeparator; returnthis._IsNeedThousandSeparator; } } set { ViewState[this.ID + "_IsNeedThousandSeparator"] = value; } } privateint _DecimalPlaces = 2; [TypeConverter(typeof(Int32Converter))] publicint DecimalPlaces { get { if (ViewState[this.ID + "_DecimalPlaces"] != null) { return (int)ViewState[this.ID + "_DecimalPlaces"]; } else { ViewState[this.ID + "_DecimalPlaces"] = this._DecimalPlaces; returnthis._DecimalPlaces; } } set { ViewState[this.ID + "_DecimalPlaces"] = value; } } privatebool _IsNeedDollarSignal = false; [TypeConverter(typeof(BooleanConverter))] publicbool IsNeedDollarSignal { get { if (ViewState[this.ID + "_IsNeedDollarSignal"] != null) { return (bool)ViewState[this.ID + "_IsNeedDollarSignal"]; } else { ViewState[this.ID + "_IsNeedDollarSignal"] = this._IsNeedDollarSignal; returnthis._IsNeedDollarSignal; } } set { ViewState[this.ID + "_IsNeedDollarSignal"] = value; } } privatestring _DollarSignal = "$"; [TypeConverter(typeof(StringConverter))] publicstring DollarSignal { get { if (ViewState[this.ID + "_DollarSignal"] != null) { return (string)ViewState[this.ID + "_DollarSignal"]; } else { ViewState[this.ID + "_DollarSignal"] = this._DollarSignal; returnthis._DollarSignal; } } set { ViewState[this.ID + "_DollarSignal"] = value; } } privatestring 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; } } privatestring _RawText = string.Empty; publicstring RawText { get { if (ViewState[this.ID + "_RawText"] != null) { return (string)ViewState[this.ID + "_RawText"]; } else { ViewState[this.ID + "_RawText"] = this._RawText; returnthis._RawText; } } privateset { ViewState[this.ID + "_RawText"] = value; } } #endregion#region Override properties publicoverridestring SkinID { get { if (this.IsRequired) { return"DecimalRequired"; } else { return"Decimal"; } } set { base.SkinID = value; } } publicoverridestring Text { get { return ToDecimalFormatString(base.Text); } set { this.RawText = value; base.Text = ToDecimalString(value); } } #endregion#region New functions protectedstring ToDecimalString(string orignal) { Regex reg = new Regex(this.ValidationExpression); if (reg.IsMatch(orignal)) { return orignal.Replace(",", "").Replace("$", ""); } else { returnstring.Empty; } } protectedstring ToDecimalFormatString(string orignal) { if (!string.IsNullOrEmpty(orignal)) { returnthis.Value.ToString(FormatString); } return orignal; } #endregion } }
有一下几个自定义属性可以设置:
IsRequired="True" 是否必填, 我这里只是控制SkinID.
IsNeedThousandSeparator="True"
这里是控制是否显示千位符
DecimalPlaces="2" 这里是控制小数位数
IsNeedDollarSignal="False" 这里是控制是否显示$符号
求助:
ASP.NET自带的控件, 用Toolbox拖入, 某些属性例如"Text"会显示在插入的控件页面代码上
若我现在有一个自定义的属性想在拖入页面的时候, 就在页面代码上就出现(包括默认值). 不知道如何实现, 希望高人指点.
原文:http://www.cnblogs.com/xachary/p/3732262.html
内容总结
以上是互联网集市为您收集整理的关于继承扩展ASP.NET控件(以Textbox为例)全部内容,希望文章能够帮你解决关于继承扩展ASP.NET控件(以Textbox为例)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。