日期:2014-05-20  浏览次数:20376 次

.net 的一个难点
在asp.net中,我想讓某个文本筐只能输入整数或小数,请问题应该怎么写呀   ?帮帮忙呀!!

------解决方案--------------------
/// <summary>
/// 只能输入数字
/// </summary>
public class CATNumTextBox : TextBox
{
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
base.OnKeyPress(e);
int I = (int)e.KeyChar;
if (I == (int)Keys.Enter || I == (int)Keys.Back || I == (int)Keys.Left ||
I == (int)Keys.Right || I == (int)Keys.Left || I == (int)Keys.Delete)
{
return;
}

char[] charNum ={ '0 ', '1 ', '2 ', '3 ', '4 ', '5 ', '6 ', '7 ', '8 ', '9 ' };
if (Array.IndexOf(charNum, e.KeyChar) < 0)
{
e.Handled = true;
}
}
}
------解决方案--------------------
千万不要使用 chendazhi(不务正业) 的方法
这种验证完全可以放到客户端 用正则表达式验证

具体做法
<asp:RegularExpressionValidator ID= "RegularExpressionValidator1 " ValidationExpression= "^((0\.[0-9]*[1-9])|([1-9][0-9]*\.[0-9]+)|([1-9][0-9]*)|(0))$ "
runat= "server " ErrorMessage= "错误提示信息! " Display= "None " ControlToValidate= "你的TEXTBOX "> </asp:RegularExpressionValidator>