Dynamic Controls and Adding it to the Form First. Why?

T

Tyler Carver

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
 
J

Jos

Tyler said:
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

I've had the same problem, and I consider it a bug in ASP.NET.
It has to do with the automatic ID's that are assigned to
dynamic controls. Only in the first case are these ID's the same
before and after postback.

I think you can work around it by assigning the ID's yourself.
But I'm guessing that this doesn't help you, because it would
cause even more "lame code".
 
E

Eliyahu Goldin

Tyler,

First of all, I am not sure if you are on the right track. Did you consider
using either datagrid or datalist or repeater with template columns? They
are made specially for what you seem to be trying to reproduce manually.

Secondly, it helps if you specify what exactly doesn't work. Are you getting
an exception?

Eliyahu
 
T

Tyler Carver

I've had the same problem, and I consider it a bug in ASP.NET.
It has to do with the automatic ID's that are assigned to
dynamic controls. Only in the first case are these ID's the same
before and after postback.

I agree, it causes a lot of grief just because you have to do things
in a certain order. In this case it does not have anything to do with
ID's (although I know that causes another problem) as you can see in
my example above I already assigned a unique ID in both cases.
I think you can work around it by assigning the ID's yourself.
But I'm guessing that this doesn't help you, because it would
cause even more "lame code".

I actually worked around the issue by adding the table to the form
first and then adding my control to the table cell and finally
populating the data. Here is the fix in my psuedo-code:

create a new table
create a new tableRow
create a tableCell

add tableCell to tableRow
add tableRow to table
**add table to form**

foreach piece of data

create customControl
add customControl to a tableCell

**populate control with data**

continue the for loop

I would still like to know why the order is necessary.

Tyler
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top