469,646 Members | 1,567 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

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 2375
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Earl Teigrob | last post: by
6 posts views Thread by arvee | last post: by
2 posts views Thread by Vivek Sharma | last post: by
6 posts views Thread by David Hearn | last post: by
4 posts views Thread by tshad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.