UserControl, impossible to access method

  • Thread starter Thread starter Christian Ista
  • Start date Start date
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,
 
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
 
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
 
Back
Top