Home All Groups Group Topic Archive Search About

Problems with Custom Web Controls in VS 2005

Author
23 Nov 2005 8:57 PM
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;
        }

....

Author
25 Nov 2005 11:54 PM
Gellert
My Control did the same (from time to time). I was wondeing about that
too.