年前工作较为悠闲了,写个控件。
可以设置允许输入整数或者浮点数,带几个简单常用属性,使用时如果需要还可以扩展。
思路是通过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>--
%>
最后贴上源代码:https://p-blog.youkuaiyun.com/images/p_blog_youkuaiyun.com/lanwilliam/数字输入.jpg
下载后将jpg改为rar即可
本文介绍了一个自定义的数字输入控件,该控件能够限制用户输入整数或浮点数,并提供了一些常用的属性设置,如颜色、大小等。通过在ASP.NET页面中注册JavaScript代码实现对输入框的键盘事件过滤。
659

被折叠的 条评论
为什么被折叠?



