I am dynamically creating a table that contains a textbox in each cell. The table is put in a placeholder control. Everything displays perfectly. The problem is when I go to retrieve the values entered in the cells. I have the code to generate the table in a separate method called CreateTable(). In order for my program to find a table in the placeholder when I go to save, I have to run CreateTable() in a postback event AND in the PageLoad event. If I call CreateTable() in only one of those places and I try to save, it says the placeholder is empty and, therefore, I cannot save the textbox contents. I've tried calling CreateTable() from InitLoad but that doesn't work because it needs to reference values from three static controls: 1 dropdown, 1 listbox, and 1 calendar control, which I don't believe have had their viewstate rendered yet. Anyone have any ideas? It would sure be nice not to have to double the number of database calls just to make this work.
Welcome to the wonderful world of ViewState and dynamic controls :-)
If you use both (ViewState and dynamic controls) you have to recreate your controls in every request because they are needed to recreate the ViewState. Split your code in control creation code (CreateTable?) and code to fill the controls (FillTable?). The best place for the control creation code is the PageInit event. This is the part of code that has to run in every request. Place the other code wherever you need it (Postback?, LoadEvent?). This part of code has to run once. After this it is part of the ViewState.
If the control creation part of your code depends on the ViewState (values of your static controls) you are in bigger trouble. You can't access the ViewState in PageInit! Use another storage methode (SessionState?) to persist this information and redirect to your own page.