-1

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?

Community
  • 1
  • 1
  • maybe you should edit your question, starting with the title, as whenever `NullReferenceException` appears it would be closed as a duplicate. If I understand you, what you want to know is how accessing a control by id,maybe you should put that – Pikoh Mar 13 '17 at 16:11
  • 2
    That's better. I nominated this to be reopened, although i don't see too many questions reopened :) – Pikoh Mar 13 '17 at 16:16
  • 1
    @Pikoh Thanks, seems like a reasonable suggestion, thanks for nominating for re-opening as well - honestly, it seems like people's knee-jerk reaction is to mark any question about any null reference exception a duplicate of that particular question, I'm not sure why. – EJoshuaS - Stand with Ukraine Mar 13 '17 at 16:18
  • 1
    @EJoshuaS Your question is really about the asp.net page lifecycle, not about how to "fix" a NRE (which the original dupe discusses in length). It's not clear in your question exactly when these single lines of code are executing (overriding OnLoad? In an event handler?), so it could use an [edit] to add more context. –  Mar 13 '17 at 16:45
  • @Will I edited to clarify that the line in question occurs in a method that's being called from `Page_Load`. – EJoshuaS - Stand with Ukraine Mar 13 '17 at 17:19

1 Answers1

0

So you are doing it wrong.

Do not try to access "tblWeekCalendar" or any other element inside your control.

Implement methods that recive a values and update your control, or methods that get values when you need.

MyWeekOfTheDay week = tblWeekCalendar.GetWeekOfTheDay();
List<MyDays> days = tblWeekCalendar.GetViewDays();
List<MyMonth> months = tblWeekCalendar.GetCurrentYear().GetMonths();

Something like that.

Henryk Budzinski
  • 1,161
  • 8
  • 20