maxp
maxp

Reputation: 25141

Adding control to TextBox (c# / .net)

public class TextBoxDerived : System.Web.UI.WebControls.TextBox
{
 protected override void OnLoad(EventArgs e)
 {
   this.Controls.Add(new LiteralControl("Hello"));
 }
}

The above code does not seem to do anything?

I was hoping something like

Hello
<input type"textbox" />

to be rendered in HTML.

Upvotes: 0

Views: 1197

Answers (1)

SirViver
SirViver

Reputation: 2441

A TextBox is not a CompositeControl, so it's children won't be rendered automatically.

What you could do, for example, is overwriting the Render method and manually rendering the control beforehand.

If you want to, as I assume, provide a textbox label and not some literal content, maybe using a HtmlGenericControl with a span or div tag would be more suitable, in order to automatically render escaped text.

protected override void Render(HtmlTextWriter writer)
{
    var label = new HtmlGenericControl("span");
    label.InnerText = "Hello";
    label.RenderControl(writer);

    base.Render(writer);
}

Upvotes: 1

Related Questions