Professional Web Applications Themes

Possible to create a composite control that has a child control that is a validator that validates the composite control itself? - ASP.NET Building Controls

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 ...

  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. #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" <jemilleruchicago.edu> wrote in message
    news:O7JDJwDcEHA.3716TK2MSFTNGP11.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

  3. #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" <jemilleruchicago.edu> wrote in message
    news:uA3Si3DcEHA.3016tk2msftngp13.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" <jemilleruchicago.edu> wrote in message
    > news:O7JDJwDcEHA.3716TK2MSFTNGP11.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

Similar Threads

  1. Composite Control - How to Expose a Child Control
    By dsmolen@gmail.com in forum ASP.NET Building Controls
    Replies: 0
    Last Post: September 28th, 07:13 PM
  2. Attaching a validator to a composite control
    By Peter Young in forum ASP.NET Building Controls
    Replies: 2
    Last Post: February 2nd, 01:13 AM
  3. Composite Control - Event not firing in child control
    By Chris Simmons in forum ASP.NET Building Controls
    Replies: 3
    Last Post: October 28th, 09:45 PM
  4. Replies: 1
    Last Post: April 8th, 11:23 PM
  5. Replies: 2
    Last Post: July 2nd, 08:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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