Finding/Referencing a UserControl on a Page or MasterPage

G

Guest

I want to create a utility function that will seach the current page for one
of my UserControls by it's type. So, let's say that I have a UserControl
whose class I defined as follows:

namespace MyUtils
{
public partial class SomeUserControl : System.Web.UI.UserControl
{
...
}
}

Now, in another class "SomeUtils" I want to be able to get a reference to a
control on the current page if it's of TYPE "SomeUserControl". I want to do
it by its TYPE rather than by it's ID.

But I'm having a few problems. Here's what I have in my class so far (which,
you'll note, is partof the same namespace "MyUtils" as the UserControl). My
problems are in comments in the code below...

namespace MyUtils
{
public class SomeUtils
{
public static void DoSomethingToCtl()
{
SomeUserControl C;
// ERROR ABOVE. Says Type unknown in scope even though
// this class and the UserControl are both in this same
namespace

Page Pg = HttpContext.Current.Handler as Page;

//Even assuming I could create a variable of type
//"SomeUserControl" above, from there, I don't know
//how to search the page for a control of that TYPE. ALSO,
//Do I have to search the Page ("Pg") AND the
//MasterPage ("Pg.Master")? Or does the Page also
//get me all of the controls of its MasterPage?
}
}
}

Help much appreciated!

Alex
 
S

Steven Cheng[MSFT]

Hello Alex,

In ASP.NET 2.0, the pages and usercontrols and their codebehind classes are
dynamically compiled at runtime. However, the dynamic compilation is
possible to compile the page or usercontrols's class into separate
assemblies(due to the folder structure and location of the page and
usercontrols). Thus, in your utility class (you put in App_Code or an
external assembly), it will not see those page/usercontrol classes(compiled
in a different dynamic compiled assembly). For your scenario, you want to
reference some certain usercontrol through their concrete type, I think you
may consider defining some base usercontrol class for your usercontrols,
these base classes can be put in App_Code source files or in an external
assembly(class library project), then you can make the usercontrols(in your
web application) derive from those base classes, e.g.

=============
public partial class usercontrols_HelloUC : MyBaseUserControlClass
{
.......................
}
=============

Thus, you can reference the usercontrol instance type the base class type
in your utility class.


As for finding controls in page through their types, there is no direct
interface in ASP.NET page or control class, the only means is to loop
through each control(include page)'s Controls collection and check control
type.

e.g.

======
foreach(Control ctrl in Page.Controls)
{
//......
}

Also, if the control in in Master page, you need to navigate to the master
page's control structure through Page.Master property. However, this is
really not recommendd since it will cause poor application performance.

Just some of my suggestion. Hope this helps you some.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hey Alex,

Have you got any progress on this issue or does my last message helps you
a little? If there is anything else we can help, please feel free to post
here.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top