如何为文本框创建自定义控件

时间:2010-09-14 04:41:30

标签: c# asp.net textbox custom-controls

Hai Freinds

到目前为止,我已经使用这个编码来创建一个自定义控件作为我在以前的帖子中的知识。我不知道它是否正确。我需要进一步的帮助。到目前为止我已经使用了这个编码appcode-&gt ; Number.cs,我已经进一步使用这个编码我应该做什么:我的要求是我拖动控件中的文本框只应输入数字。供参考,请参阅我之前的帖子:http://www.eggheadcafe.com/community/aspnet/2/10200401/how-to-create-a-dll-file-for-the-textbox-with-some-requriments.aspx.i在Web应用程序中需要它

using System;  
using System.Data;  
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;    
using System.Web.UI;    
using System.Web.UI.HtmlControls;    
using System.Web.UI.WebControls;    
using System.Web.UI.WebControls.WebParts;    
using System.Xml.Linq;

/// <summary>     
/// Summary description for Number    
/// </summary> 
public class Number : TextBox    
{    
    protected override void OnKeyPress(KeyPressEventArgs e)     
    {    
        base.OnKeyPress(e);     
        if (e.KeyChar >= '0' && e.KeyChar <= '9')     
        {    
            e.Handled = false;        
        }   
        else 
        {    
            e.Handled = true;     
        }  
    }   
}

1 个答案:

答案 0 :(得分:3)

我们已根据需要创建了相同类型的控件。我们使用js文件。 以下功能仅允许使用数字。

请使用此链接创建自定义控件http://www.codeproject.com/KB/custom-controls/CreateNumTextBoxControl.aspx

   function CheckOnlyInteger(e, sender, allowNegative) {
    e = e || window.event;
    //var key = (window.event) ? e.keyCode : e.which;
    var key = e ? e.keyCode : e.which;
    //alert(key);
    if (e.shiftKey) {
        if ((key > 47 && key < 58) || (key == 192) || (key >= 37 && key <= 39) || key == 0)     {
        return false;
      }
   }
   if ((key >= 48 && key <= 57) || // 0-9 numbers
    (key >= 35 && key <= 40) || // Left, Up, Right and Down
    key == 8 || // backspaceASKII
    key == 9 || // tabASKII
    key == 16 || // shift
    key == 17 || // control
    (key >= 96 && key <= 105) || (key == 46)) // || // Home  key == 46) // dotASKII
{
    return true;
}
 else if ((key == 45) && (allowNegative == 'true')) { // dash (-)       
    if (sender.value.indexOf('-') > -1) {
        return false;
    }
     else {
        if (sender.value.indexOf('-') == 0) {
            return true;
         }
         else {
            sender.value = sender.value.replace('-', '');
            sender.value = "-" + sender.value;
            return true;
         } //alert(key + ' ' + sender.value + ' ' + allowNegative);    alert((key == 45) && (allowNegative == true));
    }
}
else
    return false;
 }


  public partial class ANTextBox: System.Web.UI.WebControls.TextBox 
    {
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        ClientScriptManager scriptMananger = this.Page.ClientScript;
        string resourceFilePath = "TBControlLibrary.Resources.AlphaNumeric.js";

        scriptMananger.RegisterClientScriptInclude("ANTextBox", scriptMananger.GetWebResourceUrl(this.GetType(), resourceFilePath));

        if (this.Type == TextBoxType.Integer)
        {                
            if (_AllowNegative) { this.Text = "-"; }
            this.Attributes.Add("onkeydown", string.Format("return CheckOnlyInteger(event,this,'{0}');", _AllowNegative.ToString().ToLower()));               
        }
     }