Determining the container of a Control

Discussion in 'ASP .Net' started by Nathan Sokalski, Apr 5, 2009.

  1. I am writing a Validator (a control that inherits from
    System.Web.UI.WebControls.BaseValidator), and I need to get a reference to
    the Control specified by the ControlToValidate property. I have tried using:

    Me.Page.FindControl(Me.ControlToValidate)

    But this is returning the value Nothing. I think the reason for this is that
    my test page is using a Master Page, but regardless of the reason I
    obviously need code that will return the correct value regardless of where
    it is being used. After a little research, I found that the Validator and
    ControlToValidate must be in the same container (and in every scenario I
    have ever seen they are anyway, including my test page). In my case, this
    would be the ContentPlaceHolder, but for other people the container might be
    something else (Because this is a Control I need something that will work
    for everyone). Is there a property or method that can tell me the container
    of ControlToValidate (I am thinking if I use the FindControl method of the
    container, it should work)? Or, if somebody knows a better way to get a
    reference to the Control specified by ControlToValidate, that would be good
    as well. Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Apr 5, 2009
    #1
    1. Advertising

  2. Nathan Sokalski

    bruce barker Guest

    your validator should use this.FindControl() (me in vb i think). this
    will start the search from the validators naming container, instead of
    using the pages.

    if the control and the validator are not in the same naming container,
    then set the ControlToValidate to the UniqueID (in code)

    -- bruce (sqlwork.com)

    Nathan Sokalski wrote:
    > I am writing a Validator (a control that inherits from
    > System.Web.UI.WebControls.BaseValidator), and I need to get a reference to
    > the Control specified by the ControlToValidate property. I have tried using:
    >
    > Me.Page.FindControl(Me.ControlToValidate)
    >
    > But this is returning the value Nothing. I think the reason for this is that
    > my test page is using a Master Page, but regardless of the reason I
    > obviously need code that will return the correct value regardless of where
    > it is being used. After a little research, I found that the Validator and
    > ControlToValidate must be in the same container (and in every scenario I
    > have ever seen they are anyway, including my test page). In my case, this
    > would be the ContentPlaceHolder, but for other people the container might be
    > something else (Because this is a Control I need something that will work
    > for everyone). Is there a property or method that can tell me the container
    > of ControlToValidate (I am thinking if I use the FindControl method of the
    > container, it should work)? Or, if somebody knows a better way to get a
    > reference to the Control specified by ControlToValidate, that would be good
    > as well. Thanks.
    bruce barker, Apr 5, 2009
    #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. Vivi Orunitia
    Replies:
    11
    Views:
    4,443
    Martijn Lievaart
    Feb 4, 2004
  2. Maitre Bart
    Replies:
    2
    Views:
    507
    Maitre Bart
    Feb 11, 2004
  3. Steven T. Hatton
    Replies:
    4
    Views:
    3,861
    Rob Williscroft
    Dec 5, 2004
  4. Replies:
    4
    Views:
    776
    Daniel T.
    Feb 16, 2006
  5. Nathan Sokalski

    Determining the container of a Control

    Nathan Sokalski, Apr 5, 2009, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    685
    bruce barker
    Apr 5, 2009
Loading...

Share This Page