Possible to create a composite control that has a child control that is a validator that validates t

Discussion in 'ASP .Net Building Controls' started by Jonathan Eric Miller, Jul 22, 2004.

  1. I am attempting to create a composite control which has a label, followed by
    an optional error message, followed by two text boxes. I have everything
    working except the optional error message part of it. The optional error
    message is a validator that references the composite control itself. I'm
    attempting to create this control in the CreateChildControls() method.
    However, it isn't working. I'm receiving the following error message. I'm
    guessing that this is because, the control hasn't been fully created at that
    point, so, the validator can't find it. I'm wondering if anyone else has ran
    into this problem and has a workaround? I'm wondering if maybe I can add the
    validator in some other method such OnLoad() instead?

    [HttpException (0x80004005): Unable to find control id
    'inputAddress1:inputZIPCode1' referenced by the 'ControlToValidate' property
    of ''.]

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    namespace AcademicTechnologies.Controls
    {
    [ValidationPropertyAttribute("Text")]
    public class InputZIPCode : Control, INamingContainer
    {
    private bool required = false;
    private Label label1;
    private TextBox textBox1;
    private TextBox textBox2;
    public bool Required
    {
    set
    {
    required = value;
    }
    }
    public string Text
    {
    get
    {
    EnsureChildControls();
    if(textBox1.Text.Trim() == "" && textBox2.Text.Trim() == "")
    {
    return null;
    }
    string text = textBox1.Text;
    if(textBox2.Text.Trim() != "")
    {
    text += "-" + textBox2.Text;
    }
    return text;
    }
    }
    protected override void CreateChildControls()
    {
    label1 = new Label();
    label1.Text = "ZIP code: ";
    Controls.Add(label1);
    System.Diagnostics.Debug.WriteLine("ID = " + this.ID);
    System.Diagnostics.Debug.WriteLine("ClientID = " + this.ClientID);
    System.Diagnostics.Debug.WriteLine("UniqueID = " + this.UniqueID);
    if(required == true)
    {
    RequiredFieldValidator requiredFieldValidator = new
    RequiredFieldValidator();
    requiredFieldValidator.ControlToValidate = UniqueID;
    requiredFieldValidator.Display = ValidatorDisplay.Dynamic;
    requiredFieldValidator.ErrorMessage = "Value is required";
    Controls.Add(requiredFieldValidator);
    }
    textBox1 = new TextBox();
    textBox1.Width = new Unit(5, UnitType.Em);
    textBox1.MaxLength = 5;
    Controls.Add(textBox1);
    Controls.Add(new LiteralControl("-"));
    textBox2 = new TextBox();
    textBox2.Width = new Unit(4, UnitType.Em);
    textBox2.MaxLength = 4;
    Controls.Add(textBox2);
    }
    }
    }

    Jon
    Jonathan Eric Miller, Jul 22, 2004
    #1
    1. Advertising

  2. I tried it in the OnLoad() method as well and the result is the same. I'm
    wondering if maybe when it searches for the control, it doesn't include the
    parent and only includes child controls?

    I'm guessing the answer to this is, no, but, I'm wondering if there is a way
    to install the source for ASP.NET so that you can see the actual code that
    is being executed?

    Jon

    "Jonathan Eric Miller" <> wrote in message
    news:...
    > I am attempting to create a composite control which has a label, followed

    by
    > an optional error message, followed by two text boxes. I have everything
    > working except the optional error message part of it. The optional error
    > message is a validator that references the composite control itself. I'm
    > attempting to create this control in the CreateChildControls() method.
    > However, it isn't working. I'm receiving the following error message. I'm
    > guessing that this is because, the control hasn't been fully created at

    that
    > point, so, the validator can't find it. I'm wondering if anyone else has

    ran
    > into this problem and has a workaround? I'm wondering if maybe I can add

    the
    > validator in some other method such OnLoad() instead?
    >
    > [HttpException (0x80004005): Unable to find control id
    > 'inputAddress1:inputZIPCode1' referenced by the 'ControlToValidate'

    property
    > of ''.]
    >
    > using System;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > namespace AcademicTechnologies.Controls
    > {
    > [ValidationPropertyAttribute("Text")]
    > public class InputZIPCode : Control, INamingContainer
    > {
    > private bool required = false;
    > private Label label1;
    > private TextBox textBox1;
    > private TextBox textBox2;
    > public bool Required
    > {
    > set
    > {
    > required = value;
    > }
    > }
    > public string Text
    > {
    > get
    > {
    > EnsureChildControls();
    > if(textBox1.Text.Trim() == "" && textBox2.Text.Trim() == "")
    > {
    > return null;
    > }
    > string text = textBox1.Text;
    > if(textBox2.Text.Trim() != "")
    > {
    > text += "-" + textBox2.Text;
    > }
    > return text;
    > }
    > }
    > protected override void CreateChildControls()
    > {
    > label1 = new Label();
    > label1.Text = "ZIP code: ";
    > Controls.Add(label1);
    > System.Diagnostics.Debug.WriteLine("ID = " + this.ID);
    > System.Diagnostics.Debug.WriteLine("ClientID = " + this.ClientID);
    > System.Diagnostics.Debug.WriteLine("UniqueID = " + this.UniqueID);
    > if(required == true)
    > {
    > RequiredFieldValidator requiredFieldValidator = new
    > RequiredFieldValidator();
    > requiredFieldValidator.ControlToValidate = UniqueID;
    > requiredFieldValidator.Display = ValidatorDisplay.Dynamic;
    > requiredFieldValidator.ErrorMessage = "Value is required";
    > Controls.Add(requiredFieldValidator);
    > }
    > textBox1 = new TextBox();
    > textBox1.Width = new Unit(5, UnitType.Em);
    > textBox1.MaxLength = 5;
    > Controls.Add(textBox1);
    > Controls.Add(new LiteralControl("-"));
    > textBox2 = new TextBox();
    > textBox2.Width = new Unit(4, UnitType.Em);
    > textBox2.MaxLength = 4;
    > Controls.Add(textBox2);
    > }
    > }
    > }
    >
    > Jon
    >
    >
    Jonathan Eric Miller, Jul 22, 2004
    #2
    1. Advertising

  3. I just read that the controls have to be in the same container, so, I guess
    I answered my own question...

    Jon

    "Jonathan Eric Miller" <> wrote in message
    news:...
    > I tried it in the OnLoad() method as well and the result is the same. I'm
    > wondering if maybe when it searches for the control, it doesn't include

    the
    > parent and only includes child controls?
    >
    > I'm guessing the answer to this is, no, but, I'm wondering if there is a

    way
    > to install the source for ASP.NET so that you can see the actual code that
    > is being executed?
    >
    > Jon
    >
    > "Jonathan Eric Miller" <> wrote in message
    > news:...
    > > I am attempting to create a composite control which has a label,

    followed
    > by
    > > an optional error message, followed by two text boxes. I have everything
    > > working except the optional error message part of it. The optional error
    > > message is a validator that references the composite control itself. I'm
    > > attempting to create this control in the CreateChildControls() method.
    > > However, it isn't working. I'm receiving the following error message.

    I'm
    > > guessing that this is because, the control hasn't been fully created at

    > that
    > > point, so, the validator can't find it. I'm wondering if anyone else has

    > ran
    > > into this problem and has a workaround? I'm wondering if maybe I can add

    > the
    > > validator in some other method such OnLoad() instead?
    > >
    > > [HttpException (0x80004005): Unable to find control id
    > > 'inputAddress1:inputZIPCode1' referenced by the 'ControlToValidate'

    > property
    > > of ''.]
    > >
    > > using System;
    > > using System.Web.UI;
    > > using System.Web.UI.WebControls;
    > > namespace AcademicTechnologies.Controls
    > > {
    > > [ValidationPropertyAttribute("Text")]
    > > public class InputZIPCode : Control, INamingContainer
    > > {
    > > private bool required = false;
    > > private Label label1;
    > > private TextBox textBox1;
    > > private TextBox textBox2;
    > > public bool Required
    > > {
    > > set
    > > {
    > > required = value;
    > > }
    > > }
    > > public string Text
    > > {
    > > get
    > > {
    > > EnsureChildControls();
    > > if(textBox1.Text.Trim() == "" && textBox2.Text.Trim() == "")
    > > {
    > > return null;
    > > }
    > > string text = textBox1.Text;
    > > if(textBox2.Text.Trim() != "")
    > > {
    > > text += "-" + textBox2.Text;
    > > }
    > > return text;
    > > }
    > > }
    > > protected override void CreateChildControls()
    > > {
    > > label1 = new Label();
    > > label1.Text = "ZIP code: ";
    > > Controls.Add(label1);
    > > System.Diagnostics.Debug.WriteLine("ID = " + this.ID);
    > > System.Diagnostics.Debug.WriteLine("ClientID = " + this.ClientID);
    > > System.Diagnostics.Debug.WriteLine("UniqueID = " + this.UniqueID);
    > > if(required == true)
    > > {
    > > RequiredFieldValidator requiredFieldValidator = new
    > > RequiredFieldValidator();
    > > requiredFieldValidator.ControlToValidate = UniqueID;
    > > requiredFieldValidator.Display = ValidatorDisplay.Dynamic;
    > > requiredFieldValidator.ErrorMessage = "Value is required";
    > > Controls.Add(requiredFieldValidator);
    > > }
    > > textBox1 = new TextBox();
    > > textBox1.Width = new Unit(5, UnitType.Em);
    > > textBox1.MaxLength = 5;
    > > Controls.Add(textBox1);
    > > Controls.Add(new LiteralControl("-"));
    > > textBox2 = new TextBox();
    > > textBox2.Width = new Unit(4, UnitType.Em);
    > > textBox2.MaxLength = 4;
    > > Controls.Add(textBox2);
    > > }
    > > }
    > > }
    > >
    > > Jon
    > >
    > >

    >
    >
    Jonathan Eric Miller, Jul 22, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Titus A Ducksass - AKA broken-record
    Replies:
    6
    Views:
    641
    Titus A Ducksass - AKA broken-record
    Nov 15, 2004
  2. Jeff Rodriguez
    Replies:
    23
    Views:
    1,089
    David Schwartz
    Dec 9, 2003
  3. yaru22
    Replies:
    6
    Views:
    678
    Nikita the Spider
    Nov 29, 2006
  4. Radu
    Replies:
    8
    Views:
    409
  5. cwdjrxyz
    Replies:
    3
    Views:
    354
    cwdjrxyz
    Jan 23, 2009
Loading...

Share This Page