如下TextBox
<asp:textboxonkeypress="isnum()"id="TextBox1"runat="server">asp:textbox>
1. 在页面中添加一段脚本:
<scriptlanguage="javascript">
functionisnum()
{
if(event.keyCode<45||event.keyCode>57)
{
event.keyCode=0;
}
}
script>
2. 在代码中添加:
try
{
Convert.ToDouble(TextBox1.Text.Trim());
}
catch
{
Response.Write(" ");
TextBox1.Text="";
Page.RegisterStartupScript("focus"," ");
}
就ok了,是不是很简洁啊 ^_^