简单的,只允许输入数字的控件
2008-01-22 16:22
429 查看
年前工作较为悠闲了,写个控件。
可以设置允许输入整数或者浮点数,带几个简单常用属性,使用时如果需要还可以扩展。
思路是通过js过滤keydown事件。
考虑js不多,不想单独用文件,所以采用后台注册的方式。
先看下注册js的方法。
/**//// <summary>
/// 获取页面所需JS代码
/// </summary>
/// <returns></returns>
public string GetJScript()
...{
StringBuilder sb = new StringBuilder();
sb.Append("<script type="text/javascript" language="javascript">");
sb.Append(" function checkFloat(value)");
sb.Append(" {");
sb.Append(" var key = event.keyCode; ");
sb.Append(" if( (key > 95 && key < 106) || ");
sb.Append(" (key > 47 && key < 58) || ");
sb.Append(" (key == 110 && value.indexOf(".") < 0 )|| ");
sb.Append(" (key == 190 && value.indexOf(".") < 0 && value != "")){ ");
sb.Append(" ");
sb.Append(" }else if(key != 8){ ");
sb.Append(" event.returnValue = false; ");
sb.Append(" }");
sb.Append(" }");
sb.Append(" function checkInt(value)");
sb.Append(" {");
sb.Append(" var key = event.keyCode; ");
sb.Append(" if( (key > 95 && key < 106) || ");
sb.Append(" (key > 47 && key < 58)) { ");
sb.Append(" ");
sb.Append(" }else if(key != 8){ ");
sb.Append(" event.returnValue = false; ");
sb.Append(" }");
sb.Append(" }");
sb.Append(" function outFloat(textbox)");
sb.Append(" {");
sb.Append(" if(textbox.value.indexOf(".") > 0 && textbox.value.indexOf(".") == textbox.value.length-1)");
sb.Append(" {");
sb.Append(" textbox.value = textbox.value+"0";");
sb.Append(" }");
sb.Append(" }");
sb.Append("</script>");
return sb.ToString();
}
这个方法输出所需要的js方法。
然后是控件的属性
//设置只能输入浮点数
private bool isFloat = false;
public bool IsFloat
...{
set ...{ this.isFloat = value; }
get ...{ return isFloat; }
}
//设置只能输入整数
private bool isInt = false;
public bool IsInt
...{
set ...{ this.isInt = value; }
get ...{ return isInt; }
}
//private System.Web.UI.WebControls.Unit width = "100px";
public System.Web.UI.WebControls.Unit Width
...{
set ...{ txtInput.Width = value; }
get ...{ return txtInput.Width; }
}
//private System.Web.UI.WebControls.Unit height = 22;
public System.Web.UI.WebControls.Unit Height
...{
set ...{ txtInput.Height = value; }
get ...{ return txtInput.Height; }
}
public System.Drawing.Color ForeColor
...{
set ...{ txtInput.ForeColor = value; }
get ...{ return txtInput.ForeColor; }
}
public int MaxLength
...{
set ...{ txtInput.MaxLength = value; }
get ...{ return txtInput.MaxLength; }
}
public FontUnit FontSize
...{
set ...{ txtInput.Font.Size = value; }
get ...{ return txtInput.Font.Size; }
}
public string CssClass
...{
set ...{ txtInput.CssClass = value; }
get ...{ return txtInput.CssClass; }
}
其实很简单吧。不过也没怎么往深里想,如果有问题的话,欢迎留言。谢谢
最后是load方法
protected void Page_Load(object sender, EventArgs e)
...{
Page.RegisterStartupScript("JS", GetJScript());
if (IsInt)
...{
txtInput.Attributes.Add("onkeyDown", "checkInt(this.value)");
}
else if (IsFloat)
...{
txtInput.Attributes.Add("onkeyDown", "checkFloat(this.value)");
txtInput.Attributes.Add("onblur", "outFloat(this)");
}
}
下面给出完整代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class NumTextBox : System.Web.UI.UserControl
...{
//设置只能输入浮点数
private bool isFloat = false;
public bool IsFloat
...{
set ...{ this.isFloat = value; }
get ...{ return isFloat; }
}
//设置只能输入整数
private bool isInt = false;
public bool IsInt
...{
set ...{ this.isInt = value; }
get ...{ return isInt; }
}
//private System.Web.UI.WebControls.Unit width = "100px";
public System.Web.UI.WebControls.Unit Width
...{
set ...{ txtInput.Width = value; }
get ...{ return txtInput.Width; }
}
//private System.Web.UI.WebControls.Unit height = 22;
public System.Web.UI.WebControls.Unit Height
...{
set ...{ txtInput.Height = value; }
get ...{ return txtInput.Height; }
}
public System.Drawing.Color ForeColor
...{
set ...{ txtInput.ForeColor = value; }
get ...{ return txtInput.ForeColor; }
}
public int MaxLength
...{
set ...{ txtInput.MaxLength = value; }
get ...{ return txtInput.MaxLength; }
}
public FontUnit FontSize
...{
set ...{ txtInput.Font.Size = value; }
get ...{ return txtInput.Font.Size; }
}
public string CssClass
...{
set ...{ txtInput.CssClass = value; }
get ...{ return txtInput.CssClass; }
}
protected void Page_Load(object sender, EventArgs e)
...{
Page.RegisterStartupScript("JS", GetJScript());
if (IsInt)
...{
txtInput.Attributes.Add("onkeyDown", "checkInt(this.value)");
}
else if (IsFloat)
...{
txtInput.Attributes.Add("onkeyDown", "checkFloat(this.value)");
txtInput.Attributes.Add("onblur", "outFloat(this)");
}
}
/**//// <summary>
/// 获取页面所需JS代码
/// </summary>
/// <returns></returns>
public string GetJScript()
...{
StringBuilder sb = new StringBuilder();
sb.Append("<script type="text/javascript" language="javascript">");
sb.Append(" function checkFloat(value)");
sb.Append(" {");
sb.Append(" var key = event.keyCode; ");
sb.Append(" if( (key > 95 && key < 106) || ");
sb.Append(" (key > 47 && key < 58) || ");
sb.Append(" (key == 110 && value.indexOf(".") < 0 )|| ");
sb.Append(" (key == 190 && value.indexOf(".") < 0 && value != "")){ ");
sb.Append(" ");
sb.Append(" }else if(key != 8){ ");
sb.Append(" event.returnValue = false; ");
sb.Append(" }");
sb.Append(" }");
sb.Append(" function checkInt(value)");
sb.Append(" {");
sb.Append(" var key = event.keyCode; ");
sb.Append(" if( (key > 95 && key < 106) || ");
sb.Append(" (key > 47 && key < 58)) { ");
sb.Append(" ");
sb.Append(" }else if(key != 8){ ");
sb.Append(" event.returnValue = false; ");
sb.Append(" }");
sb.Append(" }");
sb.Append(" function outFloat(textbox)");
sb.Append(" {");
sb.Append(" if(textbox.value.indexOf(".") > 0 && textbox.value.indexOf(".") == textbox.value.length-1)");
sb.Append(" {");
sb.Append(" textbox.value = textbox.value+"0";");
sb.Append(" }");
sb.Append(" }");
sb.Append("</script>");
return sb.ToString();
}
}
页面
<%...@ Control Language="C#" AutoEventWireup="true" CodeFile="NumTextBox.ascx.cs" Inherits="NumTextBox" %>
<asp:TextBox ID="txtInput" runat="server" ></asp:TextBox><%...--<script type="text/javascript" language="javascript">
function checkFloat(value)
{
var key = event.keyCode;
if( (key > 95 && key < 106) ||
(key > 47 && key < 58) ||
(key == 110 && value.indexOf(".") < 0 )||
(key == 190 && value.indexOf(".") < 0 && value != "")){
}else if(key != 8){
event.returnValue = false;
}
}
function checkInt(value)
{
var key = event.keyCode;
if( (key > 95 && key < 106) ||
(key > 47 && key < 58)) {
}else if(key != 8){
event.returnValue = false;
}
}
function outFloat(textbox)
{
if(textbox.value.indexOf(".") == textbox.value.length-1)
{
textbox.value = textbox.value+"0";
}
}
</script>--%>
最后贴上源代码:http://p.blog.csdn.net/images/p_blog_csdn_net/lanwilliam/数字输入.jpg
下载后将jpg改为rar即可
可以设置允许输入整数或者浮点数,带几个简单常用属性,使用时如果需要还可以扩展。
思路是通过js过滤keydown事件。
考虑js不多,不想单独用文件,所以采用后台注册的方式。
先看下注册js的方法。
/**//// <summary>
/// 获取页面所需JS代码
/// </summary>
/// <returns></returns>
public string GetJScript()
...{
StringBuilder sb = new StringBuilder();
sb.Append("<script type="text/javascript" language="javascript">");
sb.Append(" function checkFloat(value)");
sb.Append(" {");
sb.Append(" var key = event.keyCode; ");
sb.Append(" if( (key > 95 && key < 106) || ");
sb.Append(" (key > 47 && key < 58) || ");
sb.Append(" (key == 110 && value.indexOf(".") < 0 )|| ");
sb.Append(" (key == 190 && value.indexOf(".") < 0 && value != "")){ ");
sb.Append(" ");
sb.Append(" }else if(key != 8){ ");
sb.Append(" event.returnValue = false; ");
sb.Append(" }");
sb.Append(" }");
sb.Append(" function checkInt(value)");
sb.Append(" {");
sb.Append(" var key = event.keyCode; ");
sb.Append(" if( (key > 95 && key < 106) || ");
sb.Append(" (key > 47 && key < 58)) { ");
sb.Append(" ");
sb.Append(" }else if(key != 8){ ");
sb.Append(" event.returnValue = false; ");
sb.Append(" }");
sb.Append(" }");
sb.Append(" function outFloat(textbox)");
sb.Append(" {");
sb.Append(" if(textbox.value.indexOf(".") > 0 && textbox.value.indexOf(".") == textbox.value.length-1)");
sb.Append(" {");
sb.Append(" textbox.value = textbox.value+"0";");
sb.Append(" }");
sb.Append(" }");
sb.Append("</script>");
return sb.ToString();
}
这个方法输出所需要的js方法。
然后是控件的属性
//设置只能输入浮点数
private bool isFloat = false;
public bool IsFloat
...{
set ...{ this.isFloat = value; }
get ...{ return isFloat; }
}
//设置只能输入整数
private bool isInt = false;
public bool IsInt
...{
set ...{ this.isInt = value; }
get ...{ return isInt; }
}
//private System.Web.UI.WebControls.Unit width = "100px";
public System.Web.UI.WebControls.Unit Width
...{
set ...{ txtInput.Width = value; }
get ...{ return txtInput.Width; }
}
//private System.Web.UI.WebControls.Unit height = 22;
public System.Web.UI.WebControls.Unit Height
...{
set ...{ txtInput.Height = value; }
get ...{ return txtInput.Height; }
}
public System.Drawing.Color ForeColor
...{
set ...{ txtInput.ForeColor = value; }
get ...{ return txtInput.ForeColor; }
}
public int MaxLength
...{
set ...{ txtInput.MaxLength = value; }
get ...{ return txtInput.MaxLength; }
}
public FontUnit FontSize
...{
set ...{ txtInput.Font.Size = value; }
get ...{ return txtInput.Font.Size; }
}
public string CssClass
...{
set ...{ txtInput.CssClass = value; }
get ...{ return txtInput.CssClass; }
}
其实很简单吧。不过也没怎么往深里想,如果有问题的话,欢迎留言。谢谢
最后是load方法
protected void Page_Load(object sender, EventArgs e)
...{
Page.RegisterStartupScript("JS", GetJScript());
if (IsInt)
...{
txtInput.Attributes.Add("onkeyDown", "checkInt(this.value)");
}
else if (IsFloat)
...{
txtInput.Attributes.Add("onkeyDown", "checkFloat(this.value)");
txtInput.Attributes.Add("onblur", "outFloat(this)");
}
}
下面给出完整代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class NumTextBox : System.Web.UI.UserControl
...{
//设置只能输入浮点数
private bool isFloat = false;
public bool IsFloat
...{
set ...{ this.isFloat = value; }
get ...{ return isFloat; }
}
//设置只能输入整数
private bool isInt = false;
public bool IsInt
...{
set ...{ this.isInt = value; }
get ...{ return isInt; }
}
//private System.Web.UI.WebControls.Unit width = "100px";
public System.Web.UI.WebControls.Unit Width
...{
set ...{ txtInput.Width = value; }
get ...{ return txtInput.Width; }
}
//private System.Web.UI.WebControls.Unit height = 22;
public System.Web.UI.WebControls.Unit Height
...{
set ...{ txtInput.Height = value; }
get ...{ return txtInput.Height; }
}
public System.Drawing.Color ForeColor
...{
set ...{ txtInput.ForeColor = value; }
get ...{ return txtInput.ForeColor; }
}
public int MaxLength
...{
set ...{ txtInput.MaxLength = value; }
get ...{ return txtInput.MaxLength; }
}
public FontUnit FontSize
...{
set ...{ txtInput.Font.Size = value; }
get ...{ return txtInput.Font.Size; }
}
public string CssClass
...{
set ...{ txtInput.CssClass = value; }
get ...{ return txtInput.CssClass; }
}
protected void Page_Load(object sender, EventArgs e)
...{
Page.RegisterStartupScript("JS", GetJScript());
if (IsInt)
...{
txtInput.Attributes.Add("onkeyDown", "checkInt(this.value)");
}
else if (IsFloat)
...{
txtInput.Attributes.Add("onkeyDown", "checkFloat(this.value)");
txtInput.Attributes.Add("onblur", "outFloat(this)");
}
}
/**//// <summary>
/// 获取页面所需JS代码
/// </summary>
/// <returns></returns>
public string GetJScript()
...{
StringBuilder sb = new StringBuilder();
sb.Append("<script type="text/javascript" language="javascript">");
sb.Append(" function checkFloat(value)");
sb.Append(" {");
sb.Append(" var key = event.keyCode; ");
sb.Append(" if( (key > 95 && key < 106) || ");
sb.Append(" (key > 47 && key < 58) || ");
sb.Append(" (key == 110 && value.indexOf(".") < 0 )|| ");
sb.Append(" (key == 190 && value.indexOf(".") < 0 && value != "")){ ");
sb.Append(" ");
sb.Append(" }else if(key != 8){ ");
sb.Append(" event.returnValue = false; ");
sb.Append(" }");
sb.Append(" }");
sb.Append(" function checkInt(value)");
sb.Append(" {");
sb.Append(" var key = event.keyCode; ");
sb.Append(" if( (key > 95 && key < 106) || ");
sb.Append(" (key > 47 && key < 58)) { ");
sb.Append(" ");
sb.Append(" }else if(key != 8){ ");
sb.Append(" event.returnValue = false; ");
sb.Append(" }");
sb.Append(" }");
sb.Append(" function outFloat(textbox)");
sb.Append(" {");
sb.Append(" if(textbox.value.indexOf(".") > 0 && textbox.value.indexOf(".") == textbox.value.length-1)");
sb.Append(" {");
sb.Append(" textbox.value = textbox.value+"0";");
sb.Append(" }");
sb.Append(" }");
sb.Append("</script>");
return sb.ToString();
}
}
页面
<%...@ Control Language="C#" AutoEventWireup="true" CodeFile="NumTextBox.ascx.cs" Inherits="NumTextBox" %>
<asp:TextBox ID="txtInput" runat="server" ></asp:TextBox><%...--<script type="text/javascript" language="javascript">
function checkFloat(value)
{
var key = event.keyCode;
if( (key > 95 && key < 106) ||
(key > 47 && key < 58) ||
(key == 110 && value.indexOf(".") < 0 )||
(key == 190 && value.indexOf(".") < 0 && value != "")){
}else if(key != 8){
event.returnValue = false;
}
}
function checkInt(value)
{
var key = event.keyCode;
if( (key > 95 && key < 106) ||
(key > 47 && key < 58)) {
}else if(key != 8){
event.returnValue = false;
}
}
function outFloat(textbox)
{
if(textbox.value.indexOf(".") == textbox.value.length-1)
{
textbox.value = textbox.value+"0";
}
}
</script>--%>
最后贴上源代码:http://p.blog.csdn.net/images/p_blog_csdn_net/lanwilliam/数字输入.jpg
下载后将jpg改为rar即可
相关文章推荐
- ASP.NET c# textbox 正则表达式 文本框只允许输入数字(验证控件RegularExpressionValidator )
- 只允许输入数字的TextBox控件
- C#的TextBox控件输入测试-只允许输入数字的测试:
- 定制C#TextBox控件中只允许输入数字的解决方法
- C#控件中只允许输入数字
- TextBox控件只允许输入数字(转)
- VC6下使编辑控件只能输入数字的简单实现
- [ASP.NET 2.0]定制C#TextBox控件中只允许输入数字的解决方法
- 让TEdit控件的内容居中或右对齐,限制位数,仅允许输入数字。
- TextBox控件只允许输入数字
- 只允许输入数字的TextBox控件
- winform 数字输入控件初稿
- iOS UITextfield只允许输入数字和字母,长度限制
- 文本框只允许输入数字
- C#中TextBox只允许输入数字或者带负号或者小数点
- 验证GridView控件在编辑状态下输入的数据是否合法(不合法,不允许更新,同时弹出提示框)
- 某个控件上不允许中文输入方式
- Js控制input只允许输入数字
- HTML input text 只允许输入数字
- WPF文本框只允许输入数字