简单的,只允许输入数字的控件

年前工作较为悠闲了,写个控件。

可以设置允许输入整数或者浮点数,带几个简单常用属性,使用时如果需要还可以扩展。

思路是通过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.csdn.net/images/p_blog_csdn_net/lanwilliam/数字输入.jpg

下载后将jpg改为rar即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值