tags:

views:

141

answers:

2

I'm trying to edit a textbox's text in a Page_Load form on an ASP.NET page, but when I try to edit TextBox1.Text it throws a NullReferenceException because the TextBox has not yet been instantiated. Specifically what I'm trying to implement is this: http://www.codeproject.com/KB/user-controls/popupcalendarcontrol.aspx but it is written for an older version of ASP.NET and does not work for me. Is there a way to instantiate the controls at the start of Page_Load? or another event that I can catch on load? With a normal windows form I would call InitializeComponent() in the constructor.

+1  A: 

There are absolutely different events you can attach to but it sounds like the page isn't loading the controls properly because they should be available by that point. You can take a look at the ASP.NET Page Life Cycle for more information.

Quintin Robinson
+1  A: 

Does the TextBox sit within a bound control, such as a FormView, GridView, DataList, etc? If so, then the control won't exist until after the databinding happens. Once that event happens, you can do something like

DirectCast(myDataList.SelectedRow.FindControl("myTextBox"), TextBox).Text

David