Checking if object on page exists

Discussion in 'ASP .Net' started by tshad, Aug 3, 2006.

  1. tshad

    tshad Guest

    How do you check to see if object exists before accessing it?

    For example, I may have an object that I make visible and invisible and I
    need to make sure it exists as I am putting this code in a User Control.

    UserLoggedOn.visible = true

    If this doesn't exists, I get an error.

    So how do I test for the object (vb.net and C#) before doing this code?

    Thanks,

    Tom
     
    tshad, Aug 3, 2006
    #1
    1. Advertisements

  2. tshad

    Guest Guest

    If(UserLoggedOn != null){
    UserLoggedOn.visible = true;
    }
     
    Guest, Aug 3, 2006
    #2
    1. Advertisements

  3. if (UserLoggedOn != null) UserLoggedOn.visible = true;

    -- bruce (sqlwork.com)
     
    bruce barker \(sqlwork.com\), Aug 3, 2006
    #3
  4. tshad

    michaelkb Guest

    Try:
    if(Page.FindControl("myobjectid")!=null) // It's there
     
    michaelkb, Aug 3, 2006
    #4
  5. tshad

    tshad Guest

    William mentions:

    If(UserLoggedOn != null){
    UserLoggedOn.visible = true;
    }

    Which is better or is it 6 of one...?

    Thanks,

    Tom
     
    tshad, Aug 3, 2006
    #5
  6. tshad

    michaelkb Guest

    Agreed...

     
    michaelkb, Aug 3, 2006
    #6
  7. tshad

    michaelkb Guest

    Well, I say that... unless you have a direct reference to the object,
    you will need to do the FindControl. If it's directly accessible by
    programmatic means, you wouldn't have to.
     
    michaelkb, Aug 3, 2006
    #7
  8. tshad

    Hans Kesting Guest

    Try:
    Not always: if "myobjectid" is a "grandchild" (child of another control
    on the page) or deeper, then FindControl will not find it as it
    searches only through the direct children.

    Hans Kesting
     
    Hans Kesting, Aug 4, 2006
    #8
    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.