2013-04-05 109 views
0

我添加了WebUserControl在運行,所以我不能使用MyWebUserControl.MyFunction(),我有一個母版和我想這一個:如何調用ASPX頁面上的ASCX頁面功能?

_MainContentPage = CType(_Page.Form.FindControl("MyContentPlaceHolder"), ContentPlaceHolder) 
CType(_MainContentPage.FindControl("MyWebUserControl"), myType) 

_MainContentPage.FindControl("MyWebUserControl")返回一個TableCell的。

我的方式添加WubUserControl:

tcValue = New TableCell()  
tcValue.Controls.Add(_Page.LoadControl("Paht/WebUserControl")) 
tcValue.ID = "MyWebUserControl" 

那麼這就是原因_MainContentPage.FindControl("MyWebUserControl")返回TableCell的 但如何讓Web控件,並調用Fcuntion。

+0

您可能必須使用反射來探測用戶控件類,然後調用該方法。 – AlwaysAProgrammer 2013-04-05 18:00:02

+0

謝謝,但我已經做到了(請參閱問題更新) – Logar314159 2013-04-05 18:27:23

+0

假設您的ascx位於佔位符中,您是否可以遍歷佔位符中的控件並執行類似'If(TypeOf(control)is MyAscx')然後DirectCast控制,MyAscx).MyMethod()'? – valverij 2013-04-05 18:40:11

回答

0

解決方法是獲取TableCell,然後找到控件,然後調用該函數。

_MainContentPage = CType(_Page.Form.FindControl("MyContentPlaceHolder"),ContentPlaceHolder) 
MyTableCell = CType(_MainContentPage.FindControl("MyWebUserControl"), TableCell) 
MyWebControl = CType(MyTableCell.Controls(0), MyType) 
MyWebControl.MyFunction() 
0

要將該函數作爲Web方法或在asp.net http處理程序中抽取該函數以全局訪問,這是多麼困難。我認爲這樣你可以做得更好,而不是使用反射和其他東西來訪問它。

+0

那麼...另一種方式來做到這一點,謝謝。 – Logar314159 2013-04-05 18:51:26