473,412 Members | 2,051 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

UserControl, impossible to access method

Hello,

I have a UserControl, added to a panel like this (in the Page_Load of
default.aspx).

Code :
Control login = LoadControl("controls/Login.ascx");
panel1.Controls.Add(login);

No problem.

In the codebehind of this UserControl, I have a public method.

but in the Page_Load I can't access it, the method is unknown
I tried this : login.MyFunction(); The method doesn't appear in the method
list and we are in the same namespace.

Do you know why ?

Thanks,
Christian,

Nov 19 '05 #1
2 1608
You need to downcast your reference to the type of class that's in the user
control's codebehind:

Control c = LoadControl("controls/Login.ascx");
Login l = c as Login;
if (l != null)
{
l.YourMethod();
}
-Brock
DevelopMentor
http://staff.develop.com/ballen
Hello,

I have a UserControl, added to a panel like this (in the Page_Load of
default.aspx).

Code :
Control login = LoadControl("controls/Login.ascx");
panel1.Controls.Add(login);
No problem.

In the codebehind of this UserControl, I have a public method.

but in the Page_Load I can't access it, the method is unknown
I tried this : login.MyFunction(); The method doesn't appear in the
method
list and we are in the same namespace.
Do you know why ?

Thanks,

Christian,


Nov 19 '05 #2
LoadControl returns a a Control type...which your specific control inherits
from. You need to cast what is returned (Control) back up to the specific
type you want:

Login login = (Login)LoadControl("controls/Login.ascx")
panel1.Controls.Add(login)
login.MyFunction();

this is assuming the type of your control is "Login"

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Christian Ista" <ml@cista.be> wrote in message
news:eE****************@TK2MSFTNGP12.phx.gbl...
Hello,

I have a UserControl, added to a panel like this (in the Page_Load of
default.aspx).

Code :
Control login = LoadControl("controls/Login.ascx");
panel1.Controls.Add(login);

No problem.

In the codebehind of this UserControl, I have a public method.

but in the Page_Load I can't access it, the method is unknown
I tried this : login.MyFunction(); The method doesn't appear in the method
list and we are in the same namespace.

Do you know why ?

Thanks,
Christian,


Nov 19 '05 #3

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

Similar topics

3
by: Tee | last post by:
Hi, I have a usercontrol with a overridable/virtual method. On the usercontrol onload, it will call this method. Now I have a form that have a few of this usercontrol. And I want to override...
10
by: Jeff | last post by:
I'm just starting to work with UserControls and have a question on the behavoir of exceptions within UserControls and VS.Net 2003. I have noticed that when a UserControl throws an exception from...
4
by: Joe Molloy | last post by:
Hi, I have a usercontrol containing mutually exclusive radiobuttons and I am trying to use a public function to set a property of the usercontrol based on which one is checked. However when I...
2
by: Fernando Lopes | last post by:
Hi. How can I access a method in the code behind of a user control from the code behind of a page or in the code behind of another usercontrol. I change the method to public and now I access from...
9
by: Anders K. Jacobsen [DK] | last post by:
Hi I have this that adds some usercontrol (UCTodays.ascx) to a placeholder foreach(A a in B){ UCTodays ucline = (UCTodays )LoadControl("UCTodays.ascx");...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
0
by: oliver | last post by:
QUESTION: How to access an object embedded in a UserControl through Javascript file? Another way to ask this question: How to execute script from a UserControl, accessing other objects in that...
4
by: Martin | last post by:
Hi everyone ! For those who haven't read my previous post, I have a problem with a UserControl. When I try to display it on a form, controls seem to appear one after one. In the following...
4
by: Gabriel | last post by:
Hello, I created a UserControl (MyUserControl.ascx). At design time I added this control the a page (MyPage.aspx). No problem when I execute the page I see it. I'd like access to some method...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...

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.