1

I have a form which, based on the answers given in the prior page, can have about 10 different variations in the combination of fields (most are the same, but several change). I decided rather than making 10 separate pages, I would try to make it dynamic. Eventually this will pull the form setup from a database, but for now I'm just trying to get the dynamic part to work. The following code kinda works, but it's giving me a weird result.

private void AddTestControls()
{
    var newbox = new TextBox();
    newbox.ID = "FirstBox";
    newbox.Text = "This is dynamic";
    newbox.CssClass = "stepHeader";
    DynamicDiv1.Controls.Add(newbox);

    var newlit = new Literal();
    newlit.ID = "FirstLit";
    newlit.Text = ".<br/>.";
    DynamicDiv1.Controls.Add(newlit);

    newbox.ID = "SecondBox";
    newbox.Text = "This is also dynamic";
    newbox.CssClass = "step";
    DynamicDiv1.Controls.Add(newbox);

}

I've stepped through it and all the properties are getting set correctly, but when the page finally renders, only the SecondBox control is visible. There is no trace of the FirstBox. If I change it so that SecondBox is its own object (newebox2 for example) then both are visible, but with how I was thinking that I would ultimately do the form from the database, this could complicate things. I don't understand why the textbox object has to be recreated in order to add it to the Div's collection of controls. Am I going about this all wrong, or just missing a step somewhere?

techturtle
  • 2,519
  • 5
  • 28
  • 54

2 Answers2

1

Your "SecondBox" are overwriting the "FirstBox" newbox since it's still holding a reference to it. Create a new TextBox for the second box:

var newbox = new TextBox();
newbox.ID = "FirstBox";
newbox.Text = "This is dynamic";
newbox.CssClass = "stepHeader";
DynamicDiv1.Controls.Add(newbox);

var newlit = new Literal();
newlit.ID = "FirstLit";
newlit.Text = ".<br/>.";
DynamicDiv1.Controls.Add(newlit);

// Create a new TextBox
var secondBox = new TextBox();
secondBox.ID = "SecondBox";
secondBox.Text = "This is also dynamic";
secondBox.CssClass = "step";
DynamicDiv1.Controls.Add(secondBox);

I'm not quite sure why this could complicate things, but what you could do is create a method for creating a textbox, if that's easier:

TextBox CreateTextBox(string id, string text, string cssClass)
{
    var box = new TextBox();
    box.ID = id;
    box.Text = text;
    box.CssClass = cssClass;
    return box;
}

And then

var newBox = CreateTextBox("FirstBox", "This is dynamic", "stepHeader");
DynamicDiv1.Controls.Add(newBox);
Mario S
  • 11,715
  • 24
  • 39
  • 47
  • Thanks for the info. I figured `.Add` meant creating a new object, not just passing a reference. I'm not sure either why I thought it would complicate it... one of those things where you're planning it out in your mind and it just doesn't make sense. I ended up shortening your method suggestion to something like `var newbox = new TextBox() {ID = "FirstBox" CssClass = "stepHeader" Text = "This is dynamic"}` since that shortens the amount of code and effects the same change. – techturtle Dec 04 '12 at 15:48
  • @techturtle Happens to everybody =) I actually would write as you did, but for illustration purposes I wrote it as a method. – Mario S Dec 04 '12 at 16:01
0

What's how it suppose to work. newbox1 is a reference so after the first time it's added to DynamicDiv1, it's there and if you change its Text, then the Text will be changed. You may find this SO useful. This SO demostrates the same issue you are having.

Community
  • 1
  • 1
Ray Cheng
  • 12,230
  • 14
  • 74
  • 137