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, Feb 12, 2008
    #1
    1. Advertisements

  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)
     
    bruce barker, Feb 12, 2008
    #2
    1. Advertisements

  3. Thanks! That worked great! It' nice to have people like you that give quick,
    yet complete, answers.
     
    Nathan Sokalski, Feb 12, 2008
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.