ASP.net Page object null when calling it from a control template

I created a control that uses the inside of the ITemplate so the user can add their own stuff.

<my:MyControl id="myControl" runat="server">
   <Content>
         //some stuff in here
   </Content>
</my:MyControl>

The Content property is a template. (This is simply simplified. This design is within greater control).

Now I have a problem: my user places his own UserControl in <Content>..</Content>. This should not be a problem, but UserControl makes a call to the QueryString of the page in the PageLoad event. And now I realized that the Page null object is null when the UserControl is inside the template, when it works, if it is outside, as usual on the page.

Does anyone have an idea what might be the issue here?

+3
source share
2 answers

I saw a case where I this.Pagereturned null from within a nested user control.

In this case, using the construct (Page)HttpContext.Current.Handlerto make the page object work fine.

+9
source

I do not know if it is used more, but maybe it is still used ...

When you instantiate the template (c InstantiateIn), is the resulting control hierarchy actually added to your MyControl collection Controls? AFAIK to make sure you have a non-zero page property.

+1
source

Source: https://habr.com/ru/post/1710986/


All Articles