I'm in the process of trying to convert a website from classic ASP to .NET and while most of it has actually gone pretty well, this one particular page is driving me insane.
It should, in theory, be simple: One input box and three dropdown lists (for day, month and year). Where I'm having the problem is in getting the year list (in particular, though the same issue applies to the day list) to populate using a for loop (so that I can program it to automatically show current year plus five).
If I use the code I've got in a regular page (either code behind or otherwise) with no master page, everything works great.
As soon as it goes into the actual website page (which does employ a master page), it gives me a "Object reference not set to an instance of an object." error.
The sub I'm running is as follows:
Sub Populate_YearList()
'Year list can be extended
Dim intYear As Integer
Dim MainContent As ContentPlaceHolder = CType(Page.Master.FindControl("ContentPlaceHolder2"), ContentPlaceHolder)
Dim DropDownList4 As DropDownList = CType(MainContent.FindControl("DropDownList4"), DropDownList)
For intYear = 2009 To 2015
DropDownList4.Items.Add(CStr(intYear))
Next
DropDownList4.Items.FindByValue(CStr(2013)).Selected = True
End Sub
Can anyone see what I've screwed up?