Wednesday, February 3, 2010

Calling method of a usercontrol from another usercontrol

10:21 AM Posted by jinto jose No comments
I need to call the method in a usercontrol from another usercontrol. Both controls are placed in same page.
If it is a web application, both controls will be in same namespace. So, we can get their names in the codebehind.

for eg.
if control1 is to be found from control2.
both control1 and control2 are in folder controls. webapplication name is test. So the namespace would be test.control2. Since, both these controls are under the same namespace, we can find the control2 in control1 and vice versa directly.
control2 2 = control1.FindControl("control21") as control2;

But, if this is a website, no namespace. So, we have to add a reference.
Add reference in control1 to find control2.
<%@ Reference Control="~/controls/control2" %>
then, in codebehind,
ASP.controls_control2 2 = this.FindControl("control21") as ASP.controls_control2;


Post a Comment