473,756 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding/Referencing a UserControl on a Page or MasterPage

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.U serControl
{
...
}
}

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 "SomeUserContro l". 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 DoSomethingToCt l()
{
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.Cur rent.Handler as Page;

//Even assuming I could create a variable of type
//"SomeUserContro l" 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
Jul 4 '06 #1
2 1652
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(compile d
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_He lloUC : MyBaseUserContr olClass
{
............... ........
}
=============

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

Jul 5 '06 #2
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.)

Jul 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1295
by: Alexander Widera | last post by:
Hi, the situation is the following: There is this masterpage: <%@ Master Language="C#" AutoEventWireup="true" CodeFile="mymaster.master.cs" Inherits="mymaster" %> <%@ Register tagprefix="myc" tagname="BasketSmall" Src="shop/basket_small.ascx" %>
3
3875
by: gary | last post by:
Hi, I am trying to reference an anchor in a user control with a url. This worked in 1.1 but no longer works in 2.0. The ascx control is located in a "/include" folder If you have a hyperlink control and you assign the navigateurl property = "../#anchor" whereby you want to add this # reference to the current url the url ends up with the #anchor twice.
3
2117
by: tdavisjr | last post by:
Ok.. Here it goes. I have a MasterPage that includes a UserControl (HeaderControl.ascx) that has the ID of Header1 in the markup in the Master Page. Now, the Head1 usercontrol has a property called Title. Then, I have a Test.aspx page that uses the Masterpage and I want to be able to acces the Title Property of the Header1 user control that is in the MasterPage.
0
1033
by: Captain Sensible | last post by:
Hello! I created a master page and load usercontrols dynmamically during runtime. So far so good. But for getting a unique frame with functionality buttons like Add, save etc. I created a usercontrol "TemplateUserControl" with a table, within this table a placeholder, where I wanna put in another user control. So I get a unique look for all plus the benefit , that I can check the permissions for the content in the Template control ( in...
2
7695
by: dawg1998 | last post by:
I have a page that creates dynamic textboxes based on the number of fields a user chooses to fill out. This process worked great when the page was standalone. However, when I move to a Content/MasterPage setup, the MasterPage Form seems to be interfering with the ability of my code to retrieve the value in the dynamic control. Are there any ideas on why this is happening or how to work-around the problem? What is the syntax to find a...
5
1955
by: Paul | last post by:
I have a MasterPage that has on it several UserControls (*ascx files). I wish to control the visibility of the controls on the content pages according to some parameter (for example on a user control that depends on a param StoreID, for the UserControl to be visible only if StoreID is in the query string.) How can I access a UserControl in a MasterPage from a Content page? I tried this, and variations thereof, but get errors: ...
1
1663
by: pierre | last post by:
Hi, I have a DataList inside a UserControl, which I use on a MasterPage. My ContentPage lists items which a user can add to his/her shopping basket. Can someone PLEASE let me know how I can "refresh" the datalist, contained within a usercontrol (which is on the MasterPage), from the ContentPage. Thanks in advance, Pierre
2
2208
by: =?Utf-8?B?RGFsZQ==?= | last post by:
I have an app with a MasterPage. In the MasterPage is a user control with several public properties and methods that I want exposed to the content page. Rather than writing several redirection methods to expose the properties, I would rather just expose the user control as a property of the MasterPage. The problem is, when I compile, I get an exception on the content page: The type 'MessageBlock.MessageBlock' is defined in an...
1
7594
by: zb | last post by:
Scenarion: In an ASPX page I have a user control embedded (actually embedded in MasterPage), based on completion of a AJAX call in the user control it needs to update a Grid view which is actually another user control. I have been able to do this by keeping the grid view user control inside an update panel with a hidden trigger button. On my AJAX call completion in the first user control I use client script to simulate a click on the...
0
9455
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9271
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10031
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9869
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.