I’m creating some text boxes on my form programmatically which I need to reference later using FindControl.
I’ve put the FindControl instruction in the page load method after the code which creates them but get an error:
Object reference not set to an instance of an object.
I assume this is because the textbox controls are not created until later in the lifecycle and therefore cannot be referenced from within Page_Load.
Can someone advise where in my code-behind I would need to place the FindControl instruction so that it can find these programmatically created text boxes?
Did you put your textbox control inside of another control such as a panel or grid? If so you need to recursively search all the controls on the page.
Here is an example of a recursive FindControl implementation: Recursive Page.FindControl. You can find many other examples by googling ‘recursive findcontrol’.