Checking if object on page exists

T

tshad

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
 
B

bruce barker \(sqlwork.com\)

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

-- bruce (sqlwork.com)
 
T

tshad

Try:
if(Page.FindControl("myobjectid")!=null) // It's there

William mentions:

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

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

Thanks,

Tom
 
M

michaelkb

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.
 
H

Hans Kesting

Try:
if(Page.FindControl("myobjectid")!=null) // It's there

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top