How can I call a method from my user control(.ascx) file in one of the class file(.cs file)
Any ideas, how can I do this?
How can I call a method from my user control(.ascx) file in one of the class file(.cs file)
Any ideas, how can I do this?
Give a reference to an instance of user-control to the class and make the user-control method public.
public class MyUserControl : UserControl
{
public void MyUCMethod()
{
}
}
public class MyClass
{
private MyUserControl myUC;
public MyClass(MyUserControl uc)
{
myUC = uc;
}
public void MyClassMethod()
{
myUC.MyUCMethod();
}
}
In the page class:
protected void Page_Load(object sender, EventArgs e)
{
MyUserControl uc = (MyUserControl)LoadControl("MyUserControl.ascx");
Controls.Add(uc);
MyClass c = new MyClass(uc);
c.MyClassMethod();
}
LoadControl is a method of System.Web.UI.Page class.