Possible to create a composite control that has a child control that is a validator that validates the composite control itself?

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Possible to create a composite control that has a child control that is a validator that validates the composite control itself?

    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 Guest

  2. Similar Questions and Discussions

    1. Composite Control - How to Expose a Child Control
      I am building a composite control that contains (among other things) a Label control. In order to allow users to set the Text, Font and other...
    2. Attaching a validator to a composite control
      I would like to have my control provide a Required property, and if this property is true, attach a RequiredFieldValidator. This approach works...
    3. Composite Control - Event not firing in child control
      Hello: I am experiencing an issue where I have a composite control (TestOuter) composed of more composite (TestInner) controls. When I am...
    4. Composite control delegating databound templated features to child <asp:repeater> control
      Hi Everybody, I'm creating a composite control in C# that basically renders a bunch of webcontrols such as <asp:Label />, <asp:Repeater />,...
    5. Using Table control in a custom composite control. Control does not render properly in design time.
      All, I have written a very simple custom composite control that includes a control of type System.Web.UI.WebControls.Table. The control...
  3. #2

    Default Re: Possible to create a composite control that has a child control that is a validator that validates the composite control itself?

    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" <jemiller@uchicago.edu> wrote in message
    news:O7JDJwDcEHA.3716@TK2MSFTNGP11.phx.gbl...
    > 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 Guest

  4. #3

    Default Re: Possible to create a composite control that has a child control that is a validator that validates the composite control itself?

    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" <jemiller@uchicago.edu> wrote in message
    news:uA3Si3DcEHA.3016@tk2msftngp13.phx.gbl...
    > 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" <jemiller@uchicago.edu> wrote in message
    > news:O7JDJwDcEHA.3716@TK2MSFTNGP11.phx.gbl...
    > > 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139