今天看到金碟的会计凭证,看到了它很人性化的金额输入框,突然兴起也想玩个,就做了这么简陋的一个.
不过这也是我第一个自定义控件,以后慢慢的完善它,达到有用的程度
其实很简单.先把代码贴出来,再介绍代码的说明
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Voucher


{
[DefaultProperty("jText")]
[ToolboxData(@"<{0}:VoucherInput jText = '000'
runat='server'></{0}:VoucherInput>")
]
public class VoucherInput : WebControl, INamingContainer

{
private TextBox _TextBox;


属性#region 属性
[
Bindable(true),
Category("Appearance"),
DefaultValue(""),
Description("金额")
]
public string jText

{
get

{
EnsureChildControls();
return _TextBox.Text;
}
set

{
EnsureChildControls();
_TextBox.Text = value;
}
}

#endregion Properties delegated to child controls


方法#region 方法
protected override void CreateChildControls()

{
Controls.Clear();
_TextBox = new TextBox();
_TextBox.ID = "TextBox1";
_TextBox.MaxLength = 15;
_TextBox.Width = 120;
_TextBox.Height = 20;
_TextBox.Attributes.Add("onkeypress", "keyPress()");
_TextBox.Style.Add(HtmlTextWriterStyle.BackgroundImage, "url(Images/text.gif)");
_TextBox.Style.Add(HtmlTextWriterStyle.Direction, "rtl");
_TextBox.Style.Add(HtmlTextWriterStyle.BorderWidth, "0");

this.Controls.Add(_TextBox);

}

protected override void Render(HtmlTextWriter writer)

{
AddAttributesToRender(writer);
_TextBox.RenderControl(writer);
writer.Write("<script language=javascript>function keyPress(){if(!(event.keyCode>=48&&event.keyCode<=57 ||event.keyCode==46||event.keyCode==45)){event.keyCode = 0;}}</script>");
}
#endregion Overriden methods

}
}
不过这也是我第一个自定义控件,以后慢慢的完善它,达到有用的程度
其实很简单.先把代码贴出来,再介绍代码的说明



















































































一、添加引用
using System.Web.UI;
using System.Web.UI.WebControls;
二、设置默认的属性和默认的标志
[DefaultProperty("jText")]
[ToolboxData(@"<{0}:VoucherInput jText = '0.00' runat='server'>") ]
他们在asp.net的设计页面上就等于<cc1:VoucherInput ID="VoucherInput1" runat="server" jText="0.00" />
三、设置TextBox的属性
背景图片、最大字符等等
四、输出javascrip脚本,控制文本框只能输入数字和小数点
五、界面如下
总结
这个还是有很大的问题,还远远打不到金碟的水平,不过这个也是我第一个自定义控件,我会不断的升级,如果真的能和金蝶做的一样,会马上发布的。