Composite Control design time behavior

J

J.Marsch

I have created a composite control that combines a Label and a Textbox,
where the label appears immediately above the textbox. This seems to work
fine at runtime, but at design time, when you drop the control onto a web
form, the control appears as a tiny, empty box on the webform. I would like
for it to render as it will look at run time (or at least close). How would
I do that?

Here's the code ( am using AddChildControls, not Render)

public class EditBoxLabelControl : System.Web.UI.WebControls.WebControl,
System.Web.UI.INamingContainer
{
protected System.Web.UI.WebControls.TextBox EditBox;
protected System.Web.UI.WebControls.Label FieldLabel;

protected override void CreateChildControls()
{
this.FieldLabel = new System.Web.UI.WebControls.Label();
this.FieldLabel.Text = "Label";
this.Controls.Add(this.FieldLabel);

System.Web.UI.WebControls.Literal literal = new
System.Web.UI.WebControls.Literal();
literal.Text = "<br>";
this.Controls.Add(literal);

this.EditBox = new System.Web.UI.WebControls.TextBox();
this.EditBox.Text = "test";
this.Controls.Add(this.EditBox);
}

}
 
A

Andy Smith

add this override to your control:
protected override void Render(HtmlTextWriter writer) {
this.EnsureChildControls();
base.Render(writer();
}

This makes sure that CreateChildControls has been called
before the control is rendered, which is good to do for
composite controls, regardless of designer support.

__
Andy Smith
Chief Code Monkey
 
J

J.Marsch

Thank you, Andy. I had just hit that myself!
-----Original Message-----
add this override to your control:
protected override void Render(HtmlTextWriter writer) {
this.EnsureChildControls();
base.Render(writer();
}

This makes sure that CreateChildControls has been called
before the control is rendered, which is good to do for
composite controls, regardless of designer support.

__
Andy Smith
Chief Code Monkey

Label
and a Textbox, least
close). How would
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top