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

Discussion in 'ASP .Net' started by Tyler Carver, Aug 18, 2004.

  1. Tyler Carver

    Tyler Carver Guest

    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
    Tyler Carver, Aug 18, 2004
    #1
    1. Advertising

  2. Tyler Carver

    Jos Guest

    Tyler Carver wrote:
    > 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".

    --

    Jos
    Jos, Aug 18, 2004
    #2
    1. Advertising

  3. 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

    "Tyler Carver" <> wrote in message
    news:...
    > 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
    Eliyahu Goldin, Aug 18, 2004
    #3
  4. Tyler Carver

    Tyler Carver Guest

    > 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
    Tyler Carver, Aug 18, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ronald
    Replies:
    2
    Views:
    823
    Robbe Morris [C# MVP]
    Jan 17, 2006
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    862
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
  4. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    454
    Eidolon
    May 4, 2004
  5. Replies:
    5
    Views:
    97
Loading...

Share This Page