Introduction How to use this controls public class BaseCustomControl : TextBox } set } set set CustomTextBox public class CustomTextBox : BaseCustomControl } set public TextBoxType TextType case TextBoxType.AlphaOnly: case TextBoxType.AlphaWithSpace: Controls.Add(reg); protected override void Render(HtmlTextWriter w) DecimalTextBox NumricTextBox protected override void OnPreRender(EventArgs e) base.OnPreRender(e);
} #endregion StringTextBox } set public bool AllowSpace set public bool AllowDigit } protected override void OnInit(EventArgs e) } script1+="var KeyID = (window.event) ? event.keyCode : e.which;"; }
This library contain four custom controls each control has the validations for required and character type
this control validate on keypress the entered char and it's accept only numbers
this control validate on keypress the entered char and it's accept only Decimal
this control validate on keypress the entered char and it's accept allpha pluse according to it's property AllowSpace and AllowDigit it will accept the digit and space
this control has type property and according to it in validate the validation will occure before submit
public enum TextBoxType
{Text = 1, AlphaWithSpace = 2, lphaOnly=3, Nmeric=4, mail = 5, RL=6 }
After drag the control on the page just set controls properties
<cc1:StringTextBox ID="StringTextBox1" runat="server" AllowDigit="true" AllowSpace="true"></cc1:StringTextBox>
cc1:CustomTextBox ID="CustomTextBox6"
runat="server" TextType="AlphaWithSpace" InvalidRequiredMessage="*" ></cc1:CustomTextBox>
all these controls have a common properties which included in BaseCustomControl
InvalidRequiredMessage, EnableClientScript, IsRequired, CssClassTxt, CssClassErr
Code Sample
{
public string InvalidRequiredMessage
{
get
{
string s = "Required";
object obj = ViewState["InvalidRequiredMessage"];
if (obj != null)
{
s = (string)obj;
}
return s;
}
set
{
ViewState["InvalidRequiredMessage"] = value;
}
}
public bool EnableClientScript
{
get
{
bool b = true;
object obj = ViewState["EnableClientScript"];
if (obj != null)
{
b = (bool)obj;
return b;
}
{
ViewState["EnableClientScript"] = value;
}
}
public bool IsRequired
{
get
{
bool b = true;
object obj = ViewState["IsRequired"];
if (obj != null)
{
b = (bool)obj;
return b;
}
{
ViewState["IsRequired"] = value;
}
}
public string CssClassTxt
{
get
{
string s = "txtCls";
object obj = ViewState["CssClassTxt"];
if (obj != null)
{
s = (string)obj;
}
return s;
}
{
ViewState["CssClassTxt"] = value;
this.CssClass = value;
}
}
public string CssClassErr
{
get
{
string s = "errCls";
object obj = ViewState["CssClassErr"];
if (obj != null)
{
s = (string)obj;
}
return s;
}
set
{
ViewState["CssClassErr"] = value;
}
}
protected override void OnInit(EventArgs e)
{
//check Required
if (IsRequired)
{
req = new RequiredFieldValidator();
req.ControlToValidate = this.ID;
req.ErrorMessage = this.InvalidRequiredMessage;
req.EnableClientScript = this.EnableClientScript;
req.Display = ValidatorDisplay.Dynamic;
req.CssClass = CssClassErr;
Controls.Add(req);
}
CssClass = CssClassTxt;
}
protected override void Render(HtmlTextWriter output)
{
base.Render(output);
if (req != null)
{
req.RenderControl(output);
}
}
}
public enum TextBoxType
{
Text = 1,
AlphaWithSpace = 2,
AlphaOnly=3,
Numeric=4,
Email = 5,
URL=6
}
{
public string InvalidDataMessage
{
get
{
string s = "Invalid Data";
object obj = ViewState["InvalidDataMessage"];
if (obj != null)
{
s = (string)obj;
return s;
}
{
ViewState["InvalidDataMessage"] = value;
}
}
{
get
{
TextBoxType t = TextBoxType.AlphaOnly;
object obj = ViewState["TextType"];
if (obj != null)
{
t = (TextBoxType)obj;
}
return t;
}
set
{
ViewState["TextType"] = value;
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
reg = new RegularExpressionValidator();
reg.ControlToValidate = this.ID;
reg.ErrorMessage = this.InvalidDataMessage;
reg.EnableClientScript = this.EnableClientScript;
reg.Display = ValidatorDisplay.Dynamic;
//check Type
switch (TextType)
{
{
reg.ValidationExpression = @"[a-zA-Z]*";
break;
}
case TextBoxType.Numeric:
{
reg.ValidationExpression = @"[0-9]*";
break;
}
{
reg.ValidationExpression = @"[a-zA-Z ]*";
break;
}
case TextBoxType.Text:
{
reg.ValidationExpression = @"[\w^\w\W^\W]*";
break;
}
case TextBoxType.Email:
{
reg.ValidationExpression = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
break;
}
case TextBoxType.URL:
{
reg.ValidationExpression = @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
break;
}
}
#endregion
}
{
base.Render(w);
if (reg != null)
{
reg.RenderControl(w);
}
}
public class DecimalTextBox : BaseCustomControl
{
protected override void OnInit(EventArgs e)
{
this.Attributes.Add("onkeypress", "javascript: return AllowDecimal(event,'"+this.ClientID+"');");
base.OnInit(e);
}
protected override void OnPreRender(EventArgs e)
{f(!this.Page.ClientScript.IsClientScriptBlockRegistered("strAllowNumeric"))
{
string script = @"function AllowDecimal(e,txt)
{
var iKeyCode = 0;
if (window.event)
{
iKeyCode = window.event.keyCode
}
else if (e)
{
iKeyCode = e.which;
}
var y = document.getElementById(txt).value;
if(iKeyCode==46 && y.indexOf('.')==-1){ return true;}
else if ((iKeyCode > 47 && iKeyCode < 58)|| iKeyCode==8)
{ return true;}
else
{return false;} };
this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "strAllowDecimal", script,true);
}
base.OnPreRender(e);
}
}
}
public class NumericTextBox : BaseCustomControl
{
protected override void OnInit(EventArgs e)
{ this.Attributes.Add("onkeypress", "javascript: return AllowNumeric(event);");
base.OnInit(e);
{if(!this.Page.ClientScript.IsClientScriptBlockRegistered("strAllowNumeric"))
{
string script = "function AllowNumeric(e)";
script += "{";
script += "var iKeyCode = 0;";
script += "if (window.event)";
script += "{iKeyCode = window.event.keyCode}";
script += "else if (e)";
script += "{iKeyCode = e.which;}";
script += "if ((iKeyCode > 47 && iKeyCode < 58) || iKeyCode == 8||iKeyCode == 0)";
script += "{ return true}";
script += "else";
script += "{return false;}";
script += "}";
this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "strAllowNumeric", script,true);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
public class StringTextBox : BaseCustomControl
{
public bool AllowAlpha
{
get
{
bool b = true;
object obj = ViewState["AllowAlpha"];
if (obj != null)
{
b = (bool)obj;
return b;
}
{
ViewState["AllowAlpha"] = value;
}
}
{
get
{
bool b = false;
object obj = ViewState["AllowSpace"];
if (obj != null)
{
b = (bool)obj;
}
return b;
}
{
ViewState["AllowSpace"] = value;
}
}
{
get
{
bool b = false;
object obj = ViewState["AllowDigit"];
if (obj != null)
{
b = (bool)obj;
return b;
}
set
{
ViewState["AllowDigit"] = value;
}
}
{
base.OnInit(e);
if ((!AllowAlpha) && AllowDigit)
{
this.Attributes.Add("onkeypress", "javascript: return AllowDigit(event," + AllowSpace.ToString().ToLower() + ");");
}
else if (AllowAlpha)
{
this.Attributes.Add("onkeypress", "javascript: return AllowAlpha(event," + AllowSpace.ToString().ToLower() + "," + AllowDigit.ToString().ToLower() + ");");
else
{
this.Enabled = false;
}
protected override void OnPreRender(EventArgs e)
{
string script1="<script language=javascript>";
script1+="function AllowAlpha(e,allowSpace,allowDigit)";
script1+="{";
script1+="if((KeyID >= 65 && KeyID <= 90)";
script1+="|| (KeyID >= 97 && KeyID <= 122)";
script1+="|| (KeyID >= 33 && KeyID <= 47)";
script1+="||(KeyID >= 58 && KeyID <= 64)";
script1+="|| (KeyID >= 91 && KeyID <= 96)";
script1+="|| (KeyID ==32 && allowSpace)";
script1+="|| (KeyID > 47 && KeyID < 58 && allowDigit)";
script1+="|| (KeyID >= 123 && KeyID <= 126))";
script1+="{return true;}";
script1+="else";
script1+="{return false;}";
script1+="}";
script1+="</script>";
this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "strAllowAlpha", script1);
if ((!AllowAlpha) && AllowDigit)
{
string script="<script language=javascript>";
script+="function AllowDigit(e,allowSpace)";
script+="{";
script+="var iKeyCode = 0;";
script+="if (window.event)";
script+="{iKeyCode = window.event.keyCode}";
script+="else if (e)";
script+="{iKeyCode = e.which;}";
script+="if ((iKeyCode > 47 && iKeyCode < 58)";
script+="|| (iKeyCode ==32 && allowSpace))";
script+="{ return true}";
script+="else";
script+="{return false;}";
script+="}";
script += "</script>";
this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "strAllowDigit", script);
base.OnPreRender(e);
}
CustomWebControlsLib
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.
Mahesh ChandPosted May 23, 2009, 10:56 AM
Keep up the good work and thank you for sharing it.