I am trying to use some dynamic controls that are built and then added
to tables. The problem that I am having is the timing of when I can
populate the controls and have the state remain after a postback. The
main question would be this:
Why does this work for maintaining state after a postback for dynamic
controls:
myText = new Label();
myText.ID = "myText";
Control form = FindControl("Form1");
form.Controls.Add(myText);
if (!IsPostBack)
{
myText.Text = "Remember this text.";
}
And this does not:
myText = new Label();
myText.ID = "myText";
if (!IsPostBack)
{
myText.Text = "Remember this text.";
}
Control form = FindControl("Form1");
form.Controls.Add(myText);
And how do I solve it so that the later can work?
This becomes a real problem when I loop through several controls that
I need to populate with data and I am adding them not to the form but
to a table that is later added to the form. Similar to this psuedo
code:
create a new table
create a new tableRow
foreach piece of data
create customControl
create a tableCell
add customControl to a tableCell
**populate control with data**
continue the for loop
add tableCell to tableRow
add tableRow to table
**add table to form**
I am aware that I can populate the customControl data after I add the
whole table to the form. But in my case that would create a lot of
extra lame code to handle an ordering issue and not really lend itself
to good programming practices. I would consider that a kludge.
Finnaly, if you have a solution to the my question "And how do I solve
it so that the later can work?" I will be most grateful for the
answer.
Thanks,
Tyler