Fly with the wind-TerryDong

.NET on the way

导航

用户控件之间的互操作

Posted on 2008-05-12 14:54  Terry Dong  阅读(298)  评论(2编辑  收藏  举报

两个UC:A.ascx ,B.ascx

在A中FindControl后B的类型是UsesrControl,是B父类,不能直接使用B中的自定义方法和属性,不过我们可以通过反射的方式可以在A中获取B中的所有信息并可以操作B的方法

 

demo code:

//get u2 instance

UserControl u2 = (UserControl)this.Parent.FindControl("UC2_1");

//set u2's properties

u2.GetType().GetProperty("relaType").SetValue(u2, relaType, null);
u2.GetType().GetProperty("relaInfo").SetValue(u2, relaInfo, null);
u2.GetType().GetProperty("Cust_ID").SetValue(u2, Cust_ID, null);

//operate u2's method

MethodInfo mi = u2.GetType().GetMethod("BindUC2");
mi.Invoke(u2, null);

 

看看效果吧。