程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> 只允許輸入數字型Textbox演示

只允許輸入數字型Textbox演示

編輯:關於ASP.NET

有一個需求,需要在GridView中批量輸入數字!

於是想到封裝一個TextBox

代碼如下:

public class NumberText : TextBox
{
private const string SMB_NUMBER_SCRIPT_ID = "{c4f7dcfd-8f9b-4fe8-8bd1-4a8a1f145c0c}";
private const string SMB_NUMBER_SCRIPT_ONLY_HOOK = "return NumberEditor_KeyPress_Handle(this)";
private const string SMB_NUMBER_SCRIPT_ONLY_SCRIPT = "<script language=\"javascript\">\n" +
"function NumberEditor_KeyPress_Handle(ctrl)\n{{" +
"if(event.keyCode == 13) \n return true;\n" +
"if(event.keyCode<48 || event.keyCode>57 )\n" +
"return false;\n else \n return true;\n}}" +
"</script>";
private void RenderJavscript()
{
if (!Page.ClientScript.IsClientScriptBlockRegistered(SMB_NUMBER_SCRIPT_ID))
{
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), SMB_NUMBER_SCRIPT_ID, string.Format(SMB_NUMBER_SCRIPT_ONLY_SCRIPT, base.ID));
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute("OnKeyPress", SMB_NUMBER_SCRIPT_ONLY_HOOK);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
RenderJavscript();
}
public NumberText()
: base()
{ }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved