1、创建一个新的类库目,将它命名为MyCustomControls
2、 添加System.Web引用:
3、 Visual Studio 将会自动添加一个名字为"Class1.cs"的文件。请将它删除,然后添加 NumericTextBox.cs 和NumericTextBoxExtention.cs两个文件类。另外创建一个名字为"Resources" 新的 文件夹,在这个文件夹中添加一个名字为"NumericTextBox.js"的JavaScript文件:
4、葎阻聞JavaScript廼秘壓怏周嶄?個延凪伏撹荷恬奉來葎"廼秘議彿坿"?萩歌堋和夕?
5、曾倖窃垂猟周脅喘栖糞?NumericTextBox窃?万断岻寂聞喘阻購囚忖partial? NumericTextBox.cs 喘栖糞?陣周議貸辞?NumericTextBoxExtention.cs 繍淫根侭嗤議忖粁才奉來。
NumericTextBox.cs?
using System;
using System.Web.UI;
[assembly: WebResource("MyCustomControls.Resources.NumericTextBox.js", "text/javascript")]
namespace MyCustomControls
{
[ToolboxData(@"<{0}:NumericTextBox Text="""" runat=""server"" ></ {0}:NumericTextBox>")]
public partial class NumericTextBox : System.Web.UI.WebControls.TextBox
{
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
ClientScriptManager scriptManager = this.Page.ClientScript;
string resourceFilePath = "MyCustomControls.Resources.NumericTextBox.js";
// This will register a Javascript block witht the name ''NumericTextBoxScript''
scriptManager.RegisterClientScriptInclude("NumericTextBoxScript", scriptManager.GetWebResourceUrl(this.GetType(), resourceFilePath));
if (this.Type == TextBoxType.Decimal)
this.Attributes.Add("onkeydown", string.Format("return CheckDecimal(this,''{0}'',''{1}'', {2})", NumberOfInteger, NumberOfFraction, _AllowNegative.ToString().ToLower()));
else if (this.Type == TextBoxType.Integer)
this.Attributes.Add("onkeydown", string.Format("return CheckInteger({0})", _AllowNegative.ToString().ToLower()));
this.Attributes.Add("onkeyup", string.Format("return CheckNegative(this)", _AllowNegative.ToString().ToLower()));
}
}
}
NumericTextBoxExtention.cs
namespace MyCustomControls
{
public partial class NumericTextBox : System.Web.UI.WebControls.TextBox
{
public enum TextBoxType
{
Integer,
Decimal
}
private int _NumberOfFraction = 0;
private int _NumberOfInteger = 0;
private bool _AllowNegative = false;
private TextBoxType _Type = TextBoxType.Integer;
public int Nu
|