Validators and IDs of other controls

Discussion in 'ASP .Net Building Controls' started by Nathan Sokalski, Feb 12, 2008.

  1. I have a validator control that I wrote by inheriting from the BaseValidator
    class. One of the properties of my class is the id of another control which
    will determine whether or not the ControlToValidate needs validated (this
    property is named conditionid). When I attempt to access this control, I use
    the following statement:

    needsvalidated = CType(Me.Page.FindControl(Me.conditionid),
    CheckBox).Checked

    However, when I submit the form, I recieve the following error, which points
    to the line in my code mentioned above:

    Object reference not set to an instance of an object.

    I know that the value for Me.conditionid is there, because I checked it
    while debugging. I believe the control with that id is available, because it
    is added declaratively in the *.aspx file. What could be the problem? (I
    don't know if it makes a difference for this problem, but I would like to
    note also that I am using a Master page, but none of the stuff I have
    mentioned is in the Master page; the line of code I mentioned above is from
    the validator that inherits BaseValidator, and the control that has the id
    Me.conditionid is in the *.aspx page) Any help would be appreciated. Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Feb 12, 2008
    #1
    1. Advertising

  2. Nathan Sokalski

    bruce barker Guest

    Page.FindControl only searches the pages Control collection. But a Control in
    Page collection can have its own. If you use a Master Page, then the master
    page control is in the Page collection. the master control has content
    controls. In the content controls are collections of the control defined in
    the content area. if you use server tables or repeators then you have more
    control collection.

    with validators the standard pattern is to require the controls and the
    validators have the same naming container, so your code should be:

    needsvalidated = CType(Me.FindControl(Me.conditionid), CheckBox).Checked

    though you really should check if find returns a null.

    -- bruce (sqlwork.com)


    "Nathan Sokalski" wrote:

    > I have a validator control that I wrote by inheriting from the BaseValidator
    > class. One of the properties of my class is the id of another control which
    > will determine whether or not the ControlToValidate needs validated (this
    > property is named conditionid). When I attempt to access this control, I use
    > the following statement:
    >
    > needsvalidated = CType(Me.Page.FindControl(Me.conditionid),
    > CheckBox).Checked
    >
    > However, when I submit the form, I recieve the following error, which points
    > to the line in my code mentioned above:
    >
    > Object reference not set to an instance of an object.
    >
    > I know that the value for Me.conditionid is there, because I checked it
    > while debugging. I believe the control with that id is available, because it
    > is added declaratively in the *.aspx file. What could be the problem? (I
    > don't know if it makes a difference for this problem, but I would like to
    > note also that I am using a Master page, but none of the stuff I have
    > mentioned is in the Master page; the line of code I mentioned above is from
    > the validator that inherits BaseValidator, and the control that has the id
    > Me.conditionid is in the *.aspx page) Any help would be appreciated. Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    >
    >
     
    bruce barker, Feb 12, 2008
    #2
    1. Advertising

  3. Thanks! That worked great! It' nice to have people like you that give quick,
    yet complete, answers.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "bruce barker" <> wrote in message
    news:...
    > Page.FindControl only searches the pages Control collection. But a Control
    > in
    > Page collection can have its own. If you use a Master Page, then the
    > master
    > page control is in the Page collection. the master control has content
    > controls. In the content controls are collections of the control defined
    > in
    > the content area. if you use server tables or repeators then you have more
    > control collection.
    >
    > with validators the standard pattern is to require the controls and the
    > validators have the same naming container, so your code should be:
    >
    > needsvalidated = CType(Me.FindControl(Me.conditionid), CheckBox).Checked
    >
    > though you really should check if find returns a null.
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "Nathan Sokalski" wrote:
    >
    >> I have a validator control that I wrote by inheriting from the
    >> BaseValidator
    >> class. One of the properties of my class is the id of another control
    >> which
    >> will determine whether or not the ControlToValidate needs validated (this
    >> property is named conditionid). When I attempt to access this control, I
    >> use
    >> the following statement:
    >>
    >> needsvalidated = CType(Me.Page.FindControl(Me.conditionid),
    >> CheckBox).Checked
    >>
    >> However, when I submit the form, I recieve the following error, which
    >> points
    >> to the line in my code mentioned above:
    >>
    >> Object reference not set to an instance of an object.
    >>
    >> I know that the value for Me.conditionid is there, because I checked it
    >> while debugging. I believe the control with that id is available, because
    >> it
    >> is added declaratively in the *.aspx file. What could be the problem? (I
    >> don't know if it makes a difference for this problem, but I would like to
    >> note also that I am using a Master page, but none of the stuff I have
    >> mentioned is in the Master page; the line of code I mentioned above is
    >> from
    >> the validator that inherits BaseValidator, and the control that has the
    >> id
    >> Me.conditionid is in the *.aspx page) Any help would be appreciated.
    >> Thanks.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>
    >>
    >>
     
    Nathan Sokalski, Feb 12, 2008
    #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. Jeff Tolman
    Replies:
    0
    Views:
    2,989
    Jeff Tolman
    Nov 4, 2003
  2. Ratman
    Replies:
    0
    Views:
    668
    Ratman
    Sep 14, 2004
  3. Replies:
    2
    Views:
    707
    Beat Bolli
    Jun 21, 2005
  4. Nathan Sokalski

    Validators and IDs of other controls

    Nathan Sokalski, Feb 12, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    366
    Nathan Sokalski
    Feb 12, 2008
  5. Axel Dahmen

    How to Address Other Controls' IDs?

    Axel Dahmen, Nov 21, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    145
    Axel Dahmen
    Nov 22, 2005
Loading...

Share This Page