Determining the type of a UserControl (ascx) in code

G

Guest

Hi There,

I have a usercontrol that presents the majority of a page's content. Within
the ASP.NET application it appears as XXXX.PageControl, where XXXX is the
namespace of my project. There will only ever be one and onely one of these
controls per page. I want to identify the control and set a particular
property depending upon what the current page is.

I want to find this control generically, so I wrote the following code:

for each ctl as control in me.controls
if ctl.GetType is GetType(PageControl) Then
....
exit for
end if
next

But this is never true. If I examine what is returned by
ctl.GetType.ToString, I see "ASP.PageControl_ascx".

Any advice on how to get this comparison working would be appreciated!

Cheers,
Trevor
 
P

Peter Rilling

That is because ASP.NET actually inherits from the class that you create
(that is what appears on your page, not what you create). You will have to
check for the type that is actually created when the code is compiled.

You might try using something like the "is" operator. I believe this takes
into account inheritance, so "if( ctl is typeof(PageControl)" this might
return true of any class that derives from PageControl.
 
G

Guest

Hi Peter,

Thanks for your reply. Your suggestion worked. The expression "Typeof (ctl)
is PageControl" returned true when expected.

Regards,
Trevor Andrew
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top