I created a custom user control that has the following table as part of it (and I've simplified slightly to make this easier to read):
<table id="tblWeekCalendar" runat="server">
<!-- .... -->
</table>
I'm trying to create the Custom Control from Code Behind on a different page:
// I'm passing values in on the constructor as an alternative to using attributes
MasterScheduleCalendar sch = new MasterScheduleCalendar(true, 123);
However, when I try to access tblWeekCalendar
(the main table from my custom control) from the custom control's code-behind while constructing the control, I find that it is null
:
HtmlTableCell tableCell = tblWeekCalendar.FindControl($"tdR{week}Day{day}") as HtmlTableCell;
This code runs in a method that's called from Page_Load
.
I tried doing the following instead of directly calling the constructor, as in this question, but still the same problem:
MasterScheduleCalendar ctrl = (MasterScheduleCalendar)Page.LoadControl(typeof(MasterScheduleCalendar), new object[] { true, schedule.MasterScheduleID });
As mentioned previously, this occurs in a method that's being called from the Page_Load
event handler, so I'm slightly baffled as to why tblWeekCalendar
is null
at this point. Does anyone have any suggestions as to how to fix this?