Attaching a validator to a composite control

Discussion in 'ASP .Net Building Controls' started by Peter Young, Jan 29, 2005.

  1. Peter Young

    Peter Young Guest

    I would like to have my control provide a Required property, and if this
    property is true, attach a RequiredFieldValidator.

    This approach works fine for a 'simple' control, but it fails for a
    composite control, where the control is implementing INamingContainer.

    It appears that the problem is inside of the system base validator - it is
    looking for the ControlToValidate like so:
    this.NamingContainer.FindControl(name)

    In this case, the naming container is the control it's trying to find, so it
    can't find it. It needs to search the parent naming container.

    Has anyone else run into this and found a simple workaround?

    Thanks for any help,
    Pete
    Peter Young, Jan 29, 2005
    #1
    1. Advertising

  2. Peter Young

    Peter Young Guest

    Hi Peter,

    Thanks for the info. It backs up what I believed to be true. I ended up
    creating a required field validator which replicates the system's as well as
    a base validator which derives from the system's. This lets me override the
    necessary methods to correct the naming container issue. A bit of a PITA,
    but now I have a clean solution for all the controls in my assembly that
    need it.

    -Pete

    "Peter Blum" <> wrote in message
    news:#...
    > Hi Pete,
    >
    > By design, the ControlToValidate property is limited to the current naming
    > container (which you've proven.)
    >
    > The typical answer is to the confirm that all controls are in the same
    > naming container. One way you might do this is by adding the
    > RequiredFieldValidator inside your customcontrol when the Required

    property
    > is true. They will both be in the same naming container.
    > Another is to determine if your control truly needs an INamingContainer.
    > Often when a composite control creates child controls, it assigns their

    IDs
    > based on its own ID in the CreateChildControls method. So long as the page
    > developer assigns the ID property to the control before

    CreateChildControls
    > is called, everything has a unique ID and will work fine without
    > INamingContainer.
    >
    > FYI: I built a replacement to Microsoft's validators (see my tagline). It
    > overcomes numerous limitations in validation including the

    INamingContainer
    > one. On my validators, you can either assign the ID or a reference to the
    > control. When you assign a reference to the control, it can be in any

    naming
    > container. When you assign an ID it can be in the same or any parent

    naming
    > container.
    >
    > --- Peter Blum
    > www.PeterBlum.com
    > Email:
    > Creator of "Professional Validation And More" at
    > http://www.peterblum.com/vam/home.aspx
    >
    > "Peter Young" <> wrote in message
    > news:...
    > >I would like to have my control provide a Required property, and if this
    > > property is true, attach a RequiredFieldValidator.
    > >
    > > This approach works fine for a 'simple' control, but it fails for a
    > > composite control, where the control is implementing INamingContainer.
    > >
    > > It appears that the problem is inside of the system base validator - it

    is
    > > looking for the ControlToValidate like so:
    > > this.NamingContainer.FindControl(name)
    > >
    > > In this case, the naming container is the control it's trying to find,

    so
    > > it
    > > can't find it. It needs to search the parent naming container.
    > >
    > > Has anyone else run into this and found a simple workaround?
    > >
    > > Thanks for any help,
    > > Pete
    > >
    > >

    >
    >
    Peter Young, Feb 2, 2005
    #2
    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. sleigh
    Replies:
    1
    Views:
    2,685
    sleigh
    Feb 12, 2004
  2. Simon
    Replies:
    1
    Views:
    394
    Teemu Keiski
    May 17, 2004
  3. Jonathan Eric Miller
    Replies:
    2
    Views:
    332
    Jonathan Eric Miller
    Jul 22, 2004
  4. Donal McWeeney

    custom validator and ControlToValidate in a composite control...

    Donal McWeeney, Sep 28, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    218
    Donal McWeeney
    Sep 29, 2004
  5. Chad
    Replies:
    0
    Views:
    219
Loading...

Share This Page