只能输入数字的TextBox
只能接受数字
1using System;
2using System.Text;
3using System.Web;
4using System.Web.UI;
5using System.Web.UI.WebControls;
6
7
8namespace PowerAsp.NET.Comtrols
9{
10 /**//// <summary>
11 /// 作者:黄忠诚
12 /// </summary>
13 public class NumberEditor:TextBox
14 {
15 private const string scp_number_only_hook_id = "9A5788C6-D6A2-484A-9471-3E983A2433F7";
16 private const string scp_number_only_hook = "return NumberEditor_KeyPress_Handle(this);";
17 private const string scp_number_only_script ="<script> function NumberEditor_KeyPress_Handle(ctrl){ if( event.keyCode == 13 ){return true;} if( event.keyCode < 48 || event.keyCode > 57) return false; else return true; }</script>";
18
19
20 private void RenderJavaScript()
21 {
22 if(!Page.IsClientScriptBlockRegistered(scp_number_only_hook_id))
23 {
24 Page.RegisterClientScriptBlock(scp_number_only_hook_id,scp_number_only_script);
25 }
26 }
27 protected override void AddAttributesToRender(HtmlTextWriter writer)
28 {
29 base.AddAttributesToRender (writer);
30 writer.AddAttribute("OnKeyPress",scp_number_only_hook);
31 }
32
33 protected override void OnPreRender(EventArgs e)
34 {
35 base.OnPreRender (e);
36 RenderJavaScript();
37 }
38
39 public NumberEditor()
40 {
41 }
42 }
43}
1using System;
2using System.Text;
3using System.Web;
4using System.Web.UI;
5using System.Web.UI.WebControls;
6
7
8namespace PowerAsp.NET.Comtrols
9{
10 /**//// <summary>
11 /// 作者:黄忠诚
12 /// </summary>
13 public class NumberEditor:TextBox
14 {
15 private const string scp_number_only_hook_id = "9A5788C6-D6A2-484A-9471-3E983A2433F7";
16 private const string scp_number_only_hook = "return NumberEditor_KeyPress_Handle(this);";
17 private const string scp_number_only_script ="<script> function NumberEditor_KeyPress_Handle(ctrl){ if( event.keyCode == 13 ){return true;} if( event.keyCode < 48 || event.keyCode > 57) return false; else return true; }</script>";
18
19
20 private void RenderJavaScript()
21 {
22 if(!Page.IsClientScriptBlockRegistered(scp_number_only_hook_id))
23 {
24 Page.RegisterClientScriptBlock(scp_number_only_hook_id,scp_number_only_script);
25 }
26 }
27 protected override void AddAttributesToRender(HtmlTextWriter writer)
28 {
29 base.AddAttributesToRender (writer);
30 writer.AddAttribute("OnKeyPress",scp_number_only_hook);
31 }
32
33 protected override void OnPreRender(EventArgs e)
34 {
35 base.OnPreRender (e);
36 RenderJavaScript();
37 }
38
39 public NumberEditor()
40 {
41 }
42 }
43}