Problems with Custom Web Controls in VS 2005

D

DudeBori82

I'm creating a text box control which inherits a base control which
supplies a table layout and a label for the textbox. I've got it to
work decent so far, except for an error that happens sparatically with
the web control. I'll add the custom control to the webform from the
toolbar, set some properties in the properties window, and build the
solution. The control displays fine. The next time I edit that control,
it creates another instance of the table that the base control
contains, and in some cases it repeats the textbox and label.
----------------To make a long story short (too late) it duplicates
itself in the html at design time. --------------------- I don't know
why. Any help?

Here's come code:

[DefaultProperty("LabelText")]
[ToolboxData("<{0}:BaseWebControl
runat=server></{0}:BaseWebControl>")]
[ParseChildren(false)]
public class BaseWebControl : WebControl, INamingContainer
{
protected Label lbl;
protected Table tbl;

public const string REQUIRED_STRING = "<font
class='REQUIREDFIELD'>*</font>";

protected override void CreateChildControls()
{
//Set label Properties
lbl = new Label();
lbl.CssClass = "BaseWebControlLabel";
lbl.Text = LabelText;
lbl.Width = LabelWidth;

//Set Table properties
tbl = new Table();

tbl.Rows.Add(new TableRow());
tbl.Rows[0].Cells.Add(new TableCell());
tbl.Rows[0].Cells.Add(new TableCell());
tbl.Rows.Add(new TableRow());
tbl.Rows[0].Cells.Add(new TableCell());
tbl.Rows[0].Cells.Add(new TableCell());

//Set Label Cell Properties
tbl.Rows[0].Cells[0].Controls.Add(lbl);
tbl.Rows[0].Cells[0].Width = LabelWidth;
tbl.Rows[0].Cells[0].HorizontalAlign = LabelHAlign;
tbl.Rows[0].Cells[0].VerticalAlign = LabelVAlign;

Controls.Add(tbl);

base.CreateChildControls();
}

....


namespace CustomControls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:TextBoxControl
runat=server></{0}:TextBoxControl>")]
[ParseChildren(false)]
public class TextBoxControl : CustomControls.BaseWebControl
{
TextBox tb;

protected override void CreateChildControls()
{
tb = new TextBox();
tb.Text = Text;
tb.Width = TextBoxWidth;
tb.Height = TextBoxHeight;
tb.TextMode = TextBoxType;

base.CreateChildControls();

tbl.Rows[0].Cells[1].Controls.Add(tb);
tbl.Rows[0].Cells[1].Width = TextBoxWidth;
tbl.Rows[0].Cells[1].Height = TextBoxHeight;
}

....
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top