1

我想动态呈现用户控件,但我的代码没有按预期工作。代码隐藏不会被执行。这是我的渲染代码:

Dim ucControl As UserControl = LoadControl(pControl.VirtualPath & "/" & Control & ".ascx")

Dim ucSB As New StringBuilder
Dim ucSW As New StringWriter(ucSB)
Dim ucHTML As New HtmlTextWriter(ucSW)

ucControl.RenderControl(ucHTML)

谢谢您的帮助!

4

2 回答 2

0

渲染 UserContorl 时,不会调用正常的生命周期事件。此行为是设计使然。

您可以将 UserControl 转换为您的类型并显式调用方法:

Dim ucControl As MyUserControl = Ctype(LoadControl(pControl.VirtualPath & "/" & Control & ".ascx"), MyUserControl)

ucControl.Page_Load(me, EventArgs.Empty)
于 2013-05-15T08:17:53.110 回答
0

另一种选择是在页面生命周期中尽早将动态加载的控件添加到页面,以便调用事件。

于 2013-05-15T08:20:57.790 回答