使用者控制項調用父頁面的方法:
//獲得父頁面
Page p = this.Parent.Page;
Type pageType = p.GetType();
//父頁面的方法名
MethodInfo mi = pageType.GetMethod("Loading");
//執行
mi.Invoke(p, new object[] { "參數1","參數2" });
使用者控制項與使用者控制項之間調用://獲得父頁面
Page p = this.Parent.Page;
//獲得父頁面的子控制項
UserControl uc = p.FindControl("tj_ReceiptList2") as UserControl;
Type pageType = uc.GetType();
//父類方法名
MethodInfo mi = pageType.GetMethod("Loading");
//參數
mi.Invoke(uc, new object[] { "參數1", "參數2" });
使用者控制項與使用者控制項之間設定屬性:
//獲得父頁面
Page p = this.Parent.Page;
//獲得父頁面的子控制項
UserControl uc = p.FindControl("aabb1") as UserControl;
Type pageType = uc.GetType();
//父類方法名
PropertyInfo mi = pageType.GetProperty("屬性名稱");
//mi.Invoke(uc, new object[] {});
mi.SetValue(uc,屬性值,null);
補充:
1.如果你要獲得方法的傳回值,Invoke方法返回的就是方法執行的傳回值,類型是object,你轉換一下就行了!
2.標題上還寫了傳值但是代碼裡面沒有寫?呵呵 我們能方法都能獲得難道值還拿不到?再者途徑很多,這裡就不多嘴了:)
注意:
被調用父頁或其他使用者控制項的方法必須是public!