473,473 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Accessing public methods in user controls

Hi

I have a standard asp page which uses a MasterPage. The MasterPage contains
a User control. How can I access a public method in the User control from my
WebForm page? I can't move the method to another location because it
populates a Textbox in the user control page.

Thanks
Andrew


Feb 27 '07 #1
9 2628
You can access the MasterPage directly through the Page.Master property.
However, this only provides you with a generic MasterPage base class, you'll
need to cast it to your specific MasterPage like so (MyMaster)Page.Master.
You may have to adjust the permissions on your user control in the
MasterPage as it's probably declared protected. You may have to change that
to public to get it to appear but you should be able to do
((MyMaster)Page.Master).MyControl to access the control once the access is
set to the right level.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"J055" <j0**@newsgroups.nospamwrote in message
news:eJ**************@TK2MSFTNGP04.phx.gbl...
Hi

I have a standard asp page which uses a MasterPage. The MasterPage
contains a User control. How can I access a public method in the User
control from my WebForm page? I can't move the method to another location
because it populates a Textbox in the user control page.

Thanks
Andrew


Feb 27 '07 #2
Hi Mark

My UserControl classes are public partial.

e.g.

public partial class Header
{
public void MyMethod() {}
}

but I still can't see the Header class, i.e.

MasterPage master = (MasterPage)Page.Master;

master.Header // not available

What else am I doing wrong?
Thanks again
Andrew
"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:uM**************@TK2MSFTNGP02.phx.gbl...
You can access the MasterPage directly through the Page.Master property.
However, this only provides you with a generic MasterPage base class,
you'll need to cast it to your specific MasterPage like so
(MyMaster)Page.Master. You may have to adjust the permissions on your user
control in the MasterPage as it's probably declared protected. You may
have to change that to public to get it to appear but you should be able
to do ((MyMaster)Page.Master).MyControl to access the control once the
access is set to the right level.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"J055" <j0**@newsgroups.nospamwrote in message
news:eJ**************@TK2MSFTNGP04.phx.gbl...
>Hi

I have a standard asp page which uses a MasterPage. The MasterPage
contains a User control. How can I access a public method in the User
control from my WebForm page? I can't move the method to another location
because it populates a Textbox in the user control page.

Thanks
Andrew



Feb 27 '07 #3
it's not the user control that you'll worry about, it the user control
definition from whithin the masterpage. The designer file should have all
the definitions and it's probably something like

protected Header;

,of course with the namespace in there

Did you name the master page MasterPage? I'm just wondering because in your
example it probably wouldn't work since MasterPage is already defined as the
type of page. You have to cast it to your specific master page class name
otherwise it won't work and you'll just end up with a default base master
page reference.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
"J055" <j0**@newsgroups.nospamwrote in message
news:eG**************@TK2MSFTNGP02.phx.gbl...
Hi Mark

My UserControl classes are public partial.

e.g.

public partial class Header
{
public void MyMethod() {}
}

but I still can't see the Header class, i.e.

MasterPage master = (MasterPage)Page.Master;

master.Header // not available

What else am I doing wrong?
Thanks again
Andrew
"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:uM**************@TK2MSFTNGP02.phx.gbl...
>You can access the MasterPage directly through the Page.Master property.
However, this only provides you with a generic MasterPage base class,
you'll need to cast it to your specific MasterPage like so
(MyMaster)Page.Master. You may have to adjust the permissions on your
user control in the MasterPage as it's probably declared protected. You
may have to change that to public to get it to appear but you should be
able to do ((MyMaster)Page.Master).MyControl to access the control once
the access is set to the right level.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"J055" <j0**@newsgroups.nospamwrote in message
news:eJ**************@TK2MSFTNGP04.phx.gbl...
>>Hi

I have a standard asp page which uses a MasterPage. The MasterPage
contains a User control. How can I access a public method in the User
control from my WebForm page? I can't move the method to another
location because it populates a Textbox in the user control page.

Thanks
Andrew




Feb 27 '07 #4
I have seen this same behavior on occasion...having the masterpage code page
open would sometimes allow the properties/controls to be visible through
intellisense.
Feb 27 '07 #5
Hi again

I see what you mean about the MasterPage class name. I've changed it to
MyMasterPage. I think it was causing my some other problems too. However I
still can't access the the UserControl class. By default the MasterPage
class only contains a protected Page_Load method. I don't see any
definitions for the Header UC, only the declaritive code.

I've taken another approach to accessing the user control:

MyMasterPage master = (MyMasterPage)Page.Master;

UserControl uc = (UserControl)master.FindControl("Header1");

Label lbl = (Label)uc.FindControl("LoggedInUser");
I think I have to register the UserControl in my page if I want to access
it's class?

Thanks
Confused
Andrew

"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...
it's not the user control that you'll worry about, it the user control
definition from whithin the masterpage. The designer file should have all
the definitions and it's probably something like

protected Header;

,of course with the namespace in there

Did you name the master page MasterPage? I'm just wondering because in
your example it probably wouldn't work since MasterPage is already defined
as the type of page. You have to cast it to your specific master page
class name otherwise it won't work and you'll just end up with a default
base master page reference.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
"J055" <j0**@newsgroups.nospamwrote in message
news:eG**************@TK2MSFTNGP02.phx.gbl...
>Hi Mark

My UserControl classes are public partial.

e.g.

public partial class Header
{
public void MyMethod() {}
}

but I still can't see the Header class, i.e.

MasterPage master = (MasterPage)Page.Master;

master.Header // not available

What else am I doing wrong?
Thanks again
Andrew
"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:uM**************@TK2MSFTNGP02.phx.gbl...
>>You can access the MasterPage directly through the Page.Master property.
However, this only provides you with a generic MasterPage base class,
you'll need to cast it to your specific MasterPage like so
(MyMaster)Page.Master. You may have to adjust the permissions on your
user control in the MasterPage as it's probably declared protected. You
may have to change that to public to get it to appear but you should be
able to do ((MyMaster)Page.Master).MyControl to access the control once
the access is set to the right level.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"J055" <j0**@newsgroups.nospamwrote in message
news:eJ**************@TK2MSFTNGP04.phx.gbl...
Hi

I have a standard asp page which uses a MasterPage. The MasterPage
contains a User control. How can I access a public method in the User
control from my WebForm page? I can't move the method to another
location because it populates a Textbox in the user control page.

Thanks
Andrew




Feb 27 '07 #6
That's correct Andrew. Somewhere you'll need to define the reference to your
usercontrol as public.

for example, in the codebehind of the masterpage you could simply put

public MyCustomUserControl MyControl;

Of course, assuming that the control is named MyControl and that the user
control is named MyCustomuserControl. Once done you should be able to get to
it directly from the MyMasterPage class once it's compiled.

Don't worry about being confused, this issue stumps a lot of us until we get
it working well the first time.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
"J055" <j0**@newsgroups.nospamwrote in message
news:u%****************@TK2MSFTNGP03.phx.gbl...
Hi again

I see what you mean about the MasterPage class name. I've changed it to
MyMasterPage. I think it was causing my some other problems too. However I
still can't access the the UserControl class. By default the MasterPage
class only contains a protected Page_Load method. I don't see any
definitions for the Header UC, only the declaritive code.

I've taken another approach to accessing the user control:

MyMasterPage master = (MyMasterPage)Page.Master;

UserControl uc = (UserControl)master.FindControl("Header1");

Label lbl = (Label)uc.FindControl("LoggedInUser");
I think I have to register the UserControl in my page if I want to access
it's class?

Thanks
Confused
Andrew

"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...
>it's not the user control that you'll worry about, it the user control
definition from whithin the masterpage. The designer file should have all
the definitions and it's probably something like

protected Header;

,of course with the namespace in there

Did you name the master page MasterPage? I'm just wondering because in
your example it probably wouldn't work since MasterPage is already
defined as the type of page. You have to cast it to your specific master
page class name otherwise it won't work and you'll just end up with a
default base master page reference.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
"J055" <j0**@newsgroups.nospamwrote in message
news:eG**************@TK2MSFTNGP02.phx.gbl...
>>Hi Mark

My UserControl classes are public partial.

e.g.

public partial class Header
{
public void MyMethod() {}
}

but I still can't see the Header class, i.e.

MasterPage master = (MasterPage)Page.Master;

master.Header // not available

What else am I doing wrong?
Thanks again
Andrew
"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:uM**************@TK2MSFTNGP02.phx.gbl...
You can access the MasterPage directly through the Page.Master
property. However, this only provides you with a generic MasterPage
base class, you'll need to cast it to your specific MasterPage like so
(MyMaster)Page.Master. You may have to adjust the permissions on your
user control in the MasterPage as it's probably declared protected. You
may have to change that to public to get it to appear but you should be
able to do ((MyMaster)Page.Master).MyControl to access the control once
the access is set to the right level.
--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"J055" <j0**@newsgroups.nospamwrote in message
news:eJ**************@TK2MSFTNGP04.phx.gbl...
Hi
>
I have a standard asp page which uses a MasterPage. The MasterPage
contains a User control. How can I access a public method in the User
control from my WebForm page? I can't move the method to another
location because it populates a Textbox in the user control page.
>
Thanks
Andrew
>
>
>
>




Feb 28 '07 #7
Hello Andrew,

For your scenario, if you want to access a usercontrol in Master page (from
content page), you can consider define another property/method in your
master page which simply return the UserControl or the label inside the
Usercontrol .eg.

========master page code=========

public partial class Master_site : System.Web.UI.MasterPage
{
.............

//MyUC1 is the usercontrol on master page
//LabelUC property return the Label in usercontrol

public Label BannerLabel
{
get
{
return MyUC1.LabelUC;
}
}
}

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

========in content page aspx==========
you need to put <%@ MasterType %directive so that your content page
codebehind can correctly reference the master page type:

<%@ MasterType VirtualPath="~/Master/site.master" %>
and your code is simply cast Page.Master to the specific MasterPage type
and access the property or methods
protected void Button1_Click(object sender, EventArgs e)
{
Master_site master = Page.Master as Master_site;

Response.Write("<br/>master_usercontrol_label: " +
master.BannerLabel.Text);

}
=========================================
Hope this helps. If you meet any further problem get it working, please
feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

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

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

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

This posting is provided "AS IS" with no warranties, and confers no rights.
Feb 28 '07 #8
Hi Mark, Steven

Things are starting to make some sense now. I can access UserControl classes
from my MasterPage by creating public properties/methods in the MasterPage
as suggested. I'm still puzzled my a couple of things though.

1. I don't have any *.designer.cs files in any of my web projects created
with VS2005. There's nothing stored with my codebehind files. The
definitions for any controls I create in the web page seem to be hidden.
Why? Where are they?

2. I have two MasterPage's, one of which is selected at runtime in the
preinit event. They both inherit the same class, i.e. they refer to the same
codebeind file. This seemed to work until I added this to my webform
codebehind page:

MyMasterPage master = (MyMasterPage)Page.Master;

Now I get a compilation error: CS0433 The type 'MyMasterPage' exists in both
....App_Web_7joj3qzh.dll and ...App_Web_dnbpmzbp.dll

I guess because it needs to know which Page.Master type to use? I think I'm
getting there but I'd appreciate a little more advice on these subjects.

Thanks again.
Andrew
Feb 28 '07 #9
Thanks for your reply Andrew,

As for your two questions:

1. I don't have any *.designer.cs files in any of my web projects created
with VS2005. There's nothing stored with my codebehind files. The
definitions for any controls I create in the web page seem to be hidden.
Why? Where are they?
=============================================

For ASP.NET 2.0 applications, the "*.designer.cs" file only exists in "Web
Application Project" which is available in Visual Studio 2005 after you
installed service pack 1. For each aspx page or usercontrol( or some other
component like DataSet xsd), they all have some IDE/designer generated
code. For Visual Studio 2005/ASP.NET 2.0, the default web site project do
not explicitly provide a "designer.cs" file, it is dynamically generated
and compiled at runtime(new dynamic compilation feature of asp.net 2.0).
Therefore, if you're using website ASP.NET project, you will not see that
file.

#Visual Studio 2005 Web Application Projects
http://msdn2.microsoft.com/en-us/asp.net/aa336618.aspx

#Web Application Project Model Essentials (C#)
http://msdn2.microsoft.com/en-us/lib...16(VS.80).aspx

#Visual Studio 2005 Web Application Project Tutorials and Help
http://webproject.scottgu.com/Default.aspx
The "Web application project" is mainly provided for developing ASP.NET 2.0
application under a VS 2003/ASP.NET 1.X like project. You can build the
entire project to get a single precompiled assembly. You can get more info
on this project model through the above reference.
2. I have two MasterPage's, one of which is selected at runtime in the
preinit event. They both inherit the same class, i.e. they refer to the
same
codebeind file. This seemed to work until I added this to my webform
codebehind page:
=============================================
The problem here is because you're using the ASP.NET 2.0 website project
which will dynamically compile each page, and it is possible that different
pages are dynamically compiled into different assemblies. And since you
have two master pages which refer to the same codebehind, at runtime, the
following problem will occur:

1) The application runtime first compile master pageA(when master pageA is
first used), and generate an random assembly contains the master page A's
compiled class(from codebehind)

2) The application runtime then compile master pageB(when it is first
used), and try generate a new assembly which also contains the the same
class(since master page A use the identical codebehind class as master B),
that cause the conflict here.
I haven't tried this in web application project, so far, based on my
understanding, for default web site project model, I suggest you consider
change your two master pages with the following steps:
** make your two master page use different codebehind file(so that there
won't occur two dynamically compiled asseblies contains the same class)

** if you want to share some codelogic (properties or function in the
master page file), you can try defining another base class and put it into
App_Code folder, and let your two master page codebehind class derived from
this central base class
If you have anything unclear or any further questions here, please feel
free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

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

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

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

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




Mar 1 '07 #10

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

Similar topics

8
by: Piro | last post by:
I have a class that I want to make accessible to a web service. This class does some work in its constructor method and sets some class variables in its various methods. The problem I am having...
2
by: Paul | last post by:
Hi, Is it true that I can only access a user controls public properties if I dynamically load it?, i.e. I cannot access a user controls public properties if I simply register the user control on...
6
by: Earl Teigrob | last post by:
I am writing an application that dynamically loads user controls at run time based on user options. I would like to give my users the ability to build their own user controls and add them to my...
1
by: Jeff Smith | last post by:
Can I load custom web user controls dynamically and access the properties and methods without having to explicitly define custom control types (example 2 below). I have custom web control named...
6
by: arvee | last post by:
Is there a way to access controls (and their properties) in a user control? The Web Form Designer marks controls as 'Protected' which makes them inaccessable from the host form. If I mark them as...
2
by: Vivek Sharma | last post by:
Hi There, I have a situation where I wish to load the controls dynamically on the basis of user role. Hence, I am using this code. if (UserRole == "IS Administrator") { Control UC1 =...
5
by: Khalique | last post by:
Hi everyone, I Hope that someone will be able to give me a hint to the solution to my problem. I have developed a web service (vb.net) that needs to access the folders / files and copy files to...
6
by: David Hearn | last post by:
I have a property in a user control that I am setting: Private strPageName as String Public Property PageName() as String Get Return strPageName End Get Set(byVal Value as String)...
4
by: tshad | last post by:
Is there a way for a User Control to access an object (such as label or textbox) on the .aspx page that calls it? For example: x.aspx ************************************** .... Sub...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.