简体   繁体   English

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

[英]how to create a custom control for a textbox

Hai Freinds 海弗林德斯

So far i have used this coding to create a custom Control as my knowledge in the pervious post.i dono it is correct or not.I need a further help regarding this.so far i have used this coding the appcode->Number.cs in that i have used this coding further what should i do: my requriements is that i drag the textbox in the controls only number should be entered in that. 到目前为止,我已经使用此编码创建了一个自定义控件,据我以前的知识。我不知道它是否正确。我需要有关此的进一步帮助。到目前为止,我已经使用了此编码appcode-> Number.cs因为我已经进一步使用了该编码,我该怎么办:我的要求是,我将文本框拖动到控件中,仅在其中输入数字。 for reference see my previous post: http://www.eggheadcafe.com/community/aspnet/2/10200401/how-to-create-a-dll-file-for-the-textbox-with-some-requriments.aspx.i need it in web applications 供参考,请参阅我以前的文章: 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;     
        }  
    }   
}

We have created the same type of control for our need. 我们已经根据需要创建了相同类型的控件。 We used js file for that. 我们为此使用了js文件。 The following function only allows numbers. 以下功能仅允许数字。

Please use this link to create a custom control http://www.codeproject.com/KB/custom-controls/CreateNumTextBoxControl.aspx 请使用此链接创建自定义控件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()));               
        }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM