views:

26

answers:

1

Code is simplified to make it clearer.

The description

I'm writing a custom control that uses Controls.Add() to add all of the sub control it wants.

Say for example my control is called "lots_of_text_boxes" and I add a minimum number of text boxes and provide a button (Controls.Add(button)) that says "add new text box".

The control renders nicely. I click the button, the OnLoad(EventArgs e) recreates the sub controls and asp.net's magic find all my posted data and fills in the text boxes so I can read them.

btnAddNew_Click(object sender, EventArgs e) gets called to add a new textbox.

At first I tried Controls.Add(textbox) but this puts the text box at the bottom under the add button.

I could remove the button, add the text box and re add the button but I don't want to do this because it is not simple in the real case (lots of related buttons and controls intertwined).

What I want to do: Number_of_Textboxes_to_show++; Controls.Clear(); OnLoad(new EventArgs());

this wipes the control to start over and recreates it with one new ones.

The problem

because the postdata was already loaded the page is redrawn but all the data in the fields was lost. I really want a function "LoadPostData()" that goes through all the sub controls and reloads their post data.

it makes sense to me that the post data is still in memory (I hope) and all the controls on pass 2 have the same unique ID's as they did on pass 1 (and on the previous form that was posted).

Has anyone see anything like this?

+1  A: 

It sounds like the problem is just simple control placement. If the controls were being added to the place that you wanted (above the button), there would be no need to wipe the form and rebuild it, correct?

I would suggest adding a few Panel or Placeholder controls to your user control to define the layout of it. That way, your textboxes could be added to the Panel instead of the overall usercontrol, and your layout would be preserved and the controls would be where you want them.

womp
The problem is that I also have a remove this text box button beside every text box and I don't want to write anything to a database until the user hits save because I expect a lot of people to abandon their input.I can go through each control and try to update it every time I delete , or add (and there are two types of delete one for ones in the database because I have to know to delete them on save, and one for new ones that where never saves). The trouble is then that my delete buttons and add buttons need to re implement some of the logic that the onload does. I'm worried about bugs.
Jeff