UserControl, impossible to access method

C

Christian Ista

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,
 
B

Brock Allen

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();
}
 
K

Karl Seguin

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
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top