ASP.NET同頁面內【使用者控制項與父頁面】以及【使用者控制項與使用者控制項】之間方法

來源:互聯網
上載者:User

 使用者控制項調用父頁面的方法:

//獲得父頁面
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!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.