Change the Name to aTextBox and click OK.
The new project is created, and WebCustomControl1 opens in the Code Editor.
Code for a custom Label control is included in the Web Control Library template by default. We can add custom code here.
In this code , we are creating a textbox which will accept only integer as input. If user will try to input other than integer , back color of TextBox will get black and forcolor will be red. User will get a message “Input integer Dude”.
Add reference System.Drawing
The full code is as follows
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace aTextBox
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
public class aNumericTextBox : TextBox
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public override string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
// ViewState["Text"] = value;
int i = 0;
if (int.TryParse(base.Text, out i))
{
base.BackColor = Color.White;
ViewState["Text"] = value;
}
else
{
base.Text=" Only Integer dude ";
base.BackColor=Color.Black;
base.ForeColor= Color.Red;
}
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(Text);
}
}
}