When building custom web parts its essential to understand when controls will contain thier values and when they don't and how to keep variables that will contain their values throughout the web part life cycle. Especially if you would like to dynamically load one control based on another controls input.
1. In CreateChildControls, No control will contain a value, not even hidden fields. This eliminates some uses of a common practice of utilizing hidden field to maintain viewstate.
2. ViewState Parameters are great for maintaining values throughout the web part lifecycle. Initialize them in the constructor of a web part to avoid null errors.
Initializing a view state parameter:
ViewState["parameter1"] = "";
Retrieving a value from view state:
string this = ViewState["parameter1"].toString();
3. If you would like to maintain a web parts state while navigation persists in your website, you can utilize session state variables with the following code:
Adding a variable to session state:
HttpContext.Current.Session.Add("variablename1", "value1");
Retrieving a variable from session state:
writer.Write("'" + HttpContext.Current.Session["variablename1"] + "'");