The ASP.NET user instance for the user is NULL if the page code is specified on the Page_Load page.

I have a user control that I wrote and added to an ASP.NET page, and it works fine. However, I am trying to reference the property in this custom control from the code behind, on the_Load page, but cannot, because the instance-accessible variable is null.

Is this normal for user controls and Page_Load? And if so, how can I make a reference to a control instance to access its public properties? This is what I need to do before displaying the page in order to initialize some variables.

+6
source share
2 answers

Perhaps you can access your custom control from the Page_PreRender event.

Here you can find additional documentation on the page life cycle in asp.net here .

0
source

I had the same problem and it turned out that I was not registering my user control correctly.

The correct definition is:

 <%@ Register Src="PeriodControl.ascx" TagName="PeriodControl" TagPrefix="ucs" %> 

Invalid definition:

 <%@ Register TagPrefix="ucs" Namespace="MyWebsite" Assembly="MyWebsite" %> 

The only difference was to refer to the ascx file directly, and not to the control in the assembly. Go figure !?

+15
source

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


All Articles