当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASp.net 文本框(TextBox)计算,判断输入的是否是数字

ASP.NET
动态加载Js代码到Head标签中的脚本
asp.net Parameters.AddWithValue方法在SQL语句的 Where 字句中的用法
ASP.NET 运行时错误: 没有为扩展名“.asax”注册的生成提供程序修正版
Convert.ToInt32与Int32.Parse区别及Int32.TryParse
WebService出现"因 URL 意外地以 结束,请求格式无法识别"的解决方法
asp.net(C#) Xml操作(增删改查)练习
asp.net 分页sql语句(结合aspnetpager)
asp.net开发与web标准的冲突问题的一些常见解决方法
asp.net Repeater中使用if的代码
Asp.net FCKEditor 2.6.3 上传文件没有权限解决方法
C# 命名规则(挺不错的)
asp.net 动态生成控件并获取其值
使用DataGrid中扩展ItemRenderer和HeaderRenderer进行操作
asp.net Hashtable 遍历写法
asp.net GridView和DataList实现鼠标移到行行变色
C# 邮件地址是否合法的验证
.net发送邮件实现代码
ASP.Net 上传图片并生成高清晰缩略图
asp.net 事件与委托分析
C# 无限级分类的实现

ASP.NET 中的 ASp.net 文本框(TextBox)计算,判断输入的是否是数字


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 64 ::
收藏到网摘: n/a

ASp.net文本计算,文本框数字输入检测,文本框的TextChanged事件,同时在属性的Auto Post Back设置为True
复制代码 代码如下:

protected void txtQty_TextChanged(object sender, EventArgs e)
{
checkForm();
}
//检验文本信息是否合法,如果合法则开始计算
protected void checkForm()
{
try
{
if (!IsNumberic(txtQty.Text) && txtQty.Text != "")
{
checkbool = false;
Response.Write("<script>alert('数量只能为数字,请输入数字信息,谢谢合作!')</script>");
txtQty.Text = "";
txtQty.Focus();
}
else if (txtQty.Text != "")
{
Qty = int.Parse(txtQty.Text);
}
if (!IsNumberic(txtVat.Text) && txtVat.Text != "")
{
Response.Write("<script>alert('税额只能是数字,请输入数字信息,谢谢合作!')</script>");
checkbool = false;
txtVat.Text = "";
txtVat.Focus();
}
else if (txtVat.Text != "")
{
Vat = Double.Parse(txtVat.Text);
}
if (!IsNumberic(txtUnitPrice.Text) && txtUnitPrice.Text != "")
{
Response.Write("<script>alert('价格只能是数字,请输入数字信息,谢谢合作!')</script>");
checkbool = false;
txtUnitPrice.Text = "";
txtUnitPrice.Focus();
}
else if (txtUnitPrice.Text != "")
{
UnitPrice = Double.Parse(txtUnitPrice.Text);
}
if (checkbool == true)
{
if (Vat != 0 && ExVatAmount != 0)
{
AmountVat = ExVatAmount / (1 - Vat / 100);
txtAmountVat.Text = AmountVat.ToString();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// 名称:IsNumberic
/// 功能:判断输入的是否是数字
/// 参数:string oText:源文本
/// 返回值: bool true:是 false:否
/// </summary>
public bool IsNumberic(string oText)
{
try
{
//从字符串到双精度值的转换,字符串转换为Double,如果成功则返回为真,否则返回为假。
Double var1 = Convert.ToDouble(oText);
return true;
}
catch
{
return false;
}
}
}