以下是一个相对简陋的扩展, 主要是针对金额显示的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))]
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 ;
}
}
} 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 = ; [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 > )
{
pre += ".";
for (int i = ; i < this.DecimalPlaces; i++)
{
pre += "#";
}
pre += "";
}
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
}
}

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

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

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

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

求助:

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

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

backup:

//writer.AddAttribute("IsRequired", this.IsRequired.ToString());
//writer.AddAttribute("SkinID", this.SkinID.ToString());
//[DescriptionAttribute("一个基于 Textbox 的对Decimal特殊处理的派生控件")]
//[ParseChildren(false)]
//[PersistChildren(true)]
//[DefaultProperty("Value")]
//[DefaultValue(false)]
//[Description("是否必填, 将出现必填样式")]
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
//[PersistenceMode(PersistenceMode.InnerProperty)]

关于继承扩展ASP.NET控件(以Textbox为例)的更多相关文章

  1. asp.net控件开发基础(1)(转)原文更多内容

    asp.net本身提供了很多控件,提供给我们这些比较懒惰的人使用,我认为控件的作用就在此,因为我们不想重复工作,所以要创建它,这个本身便是一个需求的关系,所以学习控件开发很有意思. wrox网站上有本 ...

  2. Asp.Netserver控件开发的Grid实现(三)列编辑器

    以下是GridColumnsEditor的实现代码: GridColumnsEditor.cs using System; using System.Collections.Generic; usin ...

  3. 为ASP.NET控件加入快捷菜单

    ContextMenu Control 快捷菜单控件概述: MSDN Liabrary 中包含了几个DHTML快捷菜单的示例.分别提供了对这一功能的不能实现方法.一个快捷菜单就是在页面中任何位置的一组 ...

  4. Asp.Net控件的客户端命名

    我们在用ASP.NET写出来的网页,用浏览器来查看生成的客户端代码的时候经常看到这样的代码:GridView1_ctl101_WebUserControl1_webuserControlButton, ...

  5. asp.net <asp:Content>控件

    <asp:Content ID="Content2" ContentPlaceHolderID="CPH_MainContent" runat=" ...

  6. FineUI 基于 ExtJS 的专业 ASP.NET 控件库

    FineUI 基于 ExtJS 的专业 ASP.NET 控件库 http://www.fineui.com/

  7. ASP.NET控件<ASP:Button /> html控件<input type="button">区别联系

    ASP.NET控件<ASP:Button />-------html控件<input type="button">杨中科是这么说的:asp和input是一样 ...

  8. asp.net控件的Hyperlink控件

    Asp.net控件: Hyperlink控件:Hyperlink控件又称为超链接控件,该控件在功能上跟Html的<a herf=””>控件相似,其显示的模式为超链接的形式. 注意: Hyp ...

  9. 把某个asp.net 控件 替换成 自定义的控件

    功能:可以把某个asp.net 控件 替换成 自定义的控件 pages 的 tagMapping 元素(ASP.NET 设置架构) 定义一个标记类型的集合,这些标记类型在编译时重新映射为其他标记类型. ...

随机推荐

  1. 该如何关闭thinkphp的缓存呢?有下面几种方法可参考:

    该如何关闭thinkphp的缓存呢?有下面几种方法可参考: (1)在配置文件中关闭缓存 在你的配置文件config.php文件中加上如下两句:   复制代码代码如下: 'TMPL_CACHE_ON'  ...

  2. [Python学习笔记][第五章Python函数设计与使用]

    2016/1/29学习内容 第四章 Python函数设计与使用 之前的几页忘记保存了 很伤心 变量作用域 -一个变量已在函数外定义,如果在函数内需要修改这个变量的值,并将这个赋值结果反映到函数之外,可 ...

  3. PHP学习笔记二十七【重写】

    <?php // class Animal{ public $name; protected $price; function cry(){ echo "动物在叫....<br/ ...

  4. UIScrollView 滑动试图

    UIScrollView --->UIView //创建UIScrollView testScrollView=[[UIScrollView alloc]init]; testScrollVie ...

  5. 第1个linux命令——echo

    功能:在显示器上显示一段文字,一般起到一个提示的作用. 语法:echo [-ne][字符串]    或   echo [--help][--version] 详细说明:echo会将输入的字符串送往标准 ...

  6. [ofbiz]设置任务计划(job),提示service_item已经传递

    问题描述:设置任务计划(job),提示service_item已经传递 解决办法: 红色框内不要填写,就可以了."已经传递"是翻译的不准确,应该是"已过时",所 ...

  7. 异常处理与调试2 - 零基础入门学习Delphi51

    异常处理与调试2 让编程改变世界 Change the world by program Delphi 异常类 利用异常机制,就是允许写代码时可以假设:如果用户可以得到子程序调用或计算的结果,这些结果 ...

  8. ASP.NET MVC 4.0 学习2-留言板實現

    新增專案實現留言板功能,瞭解MVC的運行機制 1,新增專案   2,添加數據庫文件message.mdf   Ctrl+W,L 打開資料庫連接,添加存放留言的Atricle表 添加字段,後點擊&quo ...

  9. js刷新页面

    reload 方法,该方法强迫浏览器刷新当前页面. 语法:location.reload([bForceGet]) 参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里取当前页 ...

  10. GET POST方法长度限制

    GET POST方法长度限制   1.    Get方法长度限制 Http Get方法提交的数据大小长度并没有限制,HTTP协议规范没有对URL长度进行限制.这个限制是特定的浏览器及服务器对它的限制. ...