I need to create a custom TextBox control that allows user input HTML tags. I added a new property called HtmlEnabled, default is false. If it is false, it will act exactly like the original TextBox; if it is set to true, it will call Server.HtmlEncode to encode the text. I never creat a custom control, can anyone tell me what do I need to do? What function I need to override? Thanks.
I created my TextBoxEx class as following: I still get the validation error when I set HtmlEnabled to true, can anybody tell me what is wrong?
namespace WebApplication1
{
[ToolboxData("<{0}:TextBoxEx runat=server></{0}:TextBoxEx")]
public class TextBoxEx : System.Web.UI.WebControls.TextBox
{
public bool HtmlEnabled
{
get
{
return (bool)ViewState["HtmlEnabled"];
}
set
{
ViewState["HtmlEnabled"] = value;
}
}
public TextBoxEx()
{
ViewState["HtmlEnabled"] = false;
}
public override string Text
{
get
{
if (HtmlEnabled)
return HttpUtility.HtmlEncode(base.Text);
else return base.Text;
}
set
{
if (HtmlEnabled)
base.Text = HttpUtility.HtmlDecode(value);
else base.Text = value;
}
}
}
}
Sounds like you could just inherit from the TextBox control and override the Text property. This article should get you started on how to go about doing it:
https://web.archive.org/web/20211020203142/https://www.4guysfromrolla.com/articles/100103-1.aspx