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. 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
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. The following function only allows numbers.
Please use this link to create a custom control 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()));
}
}
精彩评论