今天看到金碟的会计凭证,看到了它很人性化的金额输入框,突然兴起也想玩个,就做了这么简陋的一个.
不过这也是我第一个自定义控件,以后慢慢的完善它,达到有用的程度
其实很简单.先把代码贴出来,再介绍代码的说明
using
System;
using
System.ComponentModel;
using
System.Web.UI;
using
System.Web.UI.WebControls;
namespace
Voucher
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
[DefaultProperty("jText")]
[ToolboxData(@"<{0}:VoucherInput jText = '000'
runat='server'></{0}:VoucherInput>")
]
public class VoucherInput : WebControl, INamingContainer
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private TextBox _TextBox;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
属性#region 属性
[
Bindable(true),
Category("Appearance"),
DefaultValue(""),
Description("金额")
]
public string jText
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EnsureChildControls();
return _TextBox.Text;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EnsureChildControls();
_TextBox.Text = value;
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion Properties delegated to child controls
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
方法#region 方法
protected override void CreateChildControls()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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");
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.Controls.Add(_TextBox);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected override void Render(HtmlTextWriter writer)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
一、添加引用
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脚本,控制文本框只能输入数字和小数点
五、界面如下
![10-26-1.gif](https://i-blog.csdnimg.cn/blog_migrate/6a6108b9e25c97eaff7315fb4fe84706.gif)
总结
这个还是有很大的问题,还远远打不到金碟的水平,不过这个也是我第一个自定义控件,我会不断的升级,如果真的能和金蝶做的一样,会马上发布的。