上次一时兴起,做了一个仿金蝶的凭证金额输入的控件,今天使用了下,真的奇陋无比,所以花了几个小时加强了点功能
主要有增加以下功能
1、实现整数和小数位的分开
2、在得到焦点的时候能控制在整数位
3、在输入“.”的时候可以自动跳转到小数位(红线后)
4、能设置和获得原始的数据
以下是示意图

代码如下
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Voucher


{
[DefaultProperty("jText")]
[ToolboxData(@"<{0}:VoucherInput jText = ''
runat='server'></{0}:VoucherInput>")
]
//设置默认的属性
public class VoucherInput : WebControl

{
private TextBox _TextBox; //整数位
private TextBox _PTextBox; //小数位


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

{
get

{
EnsureChildControls();
return _TextBox.Text + "." + _PTextBox.Text;
}
set

{
EnsureChildControls();
if (value == "" || value == null)

{
}
else

{
string[] Allstring = value.Split('.');
_TextBox.Text = Allstring[0];
_PTextBox.Text = Allstring[1];
}
}
}

#endregion Properties delegated to child controls


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

{
Controls.Clear();
_TextBox = new TextBox();
_TextBox.ID = "TextBox1";
_TextBox.MaxLength = 13;
_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");

_PTextBox = new TextBox();
_PTextBox.Width = 15;
_PTextBox.Height = 20;
_PTextBox.MaxLength = 2;
_PTextBox.ID = "PTextBox";
_PTextBox.Style.Add(HtmlTextWriterStyle.BackgroundImage, "url(Images/textPoint.gif)");

_PTextBox.Style.Add(HtmlTextWriterStyle.BorderWidth, "0");


this.Controls.Add(_TextBox);
this.Controls.Add(_PTextBox);

}

protected override void Render(HtmlTextWriter writer)

{
AddAttributesToRender(writer);
_TextBox.RenderControl(writer);
_PTextBox.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;}if(event.keyCode==46){event.keyCode = 0;document.getElementById('PTextBox').focus();}}</script>"); //对字符的控制
}
#endregion Overriden methods

}
}




总结
现在还是不满意,比如删数据的时候还是小数位和整数位分开发删除,目前还没有具体的想法