how to refer to a user control's "strong" type
I have code like this which loads a control from an 开发者_StackOverflow中文版.ascx file, calls some methods of the control, then renders it:
Option Explicit Off
Sub renderMyControl(p As Page)
Dim ctrl as Object = p.LoadControl("path/to/myControl.ascx")
' ... do stuff with ctrl ...
ctrl.DoThing1()
' ...
ctrl.RenderControl(New HtmlTextWriter(p.Response.Out))
End Sub
This code only works because Explicit
is off. I would like to use CType
to convert the control to its "strong type" (and get rid of Option Explicit Off
) but I don't know how to add a reference to the type. How can I do this?
VB.NET or C#? I think you got your samples mixed up a bit. :)
This will work - in VB.NET:
Sub renderMyControl(ByVal p As Page)
Dim ctrl = CType(p.LoadControl("myControl.ascx"), myControl)
ctrl.DoThing1()
ctrl.RenderControl(New HtmlTextWriter(p.Response.Output))
End Sub
精彩评论