This question seems related to How to access page controls from user control?, but I do not believe it is a duplicate.
I have many user controls that need to access the Page variable during their Render events. This works fine when I drop the controls on a .aspx page. However, when I try to nest the controls like so:
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
Dim nestedControl = New CustomControl()
helpIcon.RenderControl(writer)
MyBase.Render(writer)
End Sub
I get an exception that Variable cannot be null: Page from the Render event of CustomControl (the control being nested).
Is there an easy way to fix this so that controls constructed in another control’s code behind have access to the Page context?
You need to add the control the page’s control tree and then it will get rendered normally without having to call Render()
More likely you will add it to some container on the page: