I'm programmatically adding panels (I call this panels blocks for better understanding) to another panel. Each of these blocks contain a title, a button to add a text box to the block and one staring text box.
This is the event I use to add the text boxes:
/// <summary>
/// Adds a text box to the button's parent
/// </summary>
protected void AddLabel_Click(object sender, EventArgs e)
{
Button senderButton = (Button)sender;
string parentId = senderButton.ID.Replace("_button","");
Panel parent = (Panel)FindControl(update_panel, parentId);
parent.Controls.Add(new TextBox
{
CssClass = "form-control canvas-label",
ID = parent.ID + "_label" + parent.Controls.OfType<TextBox>().Count<TextBox>()
});
}
However, every time I add a text box, the one I just created gets deleted
Edit This is how I ended up solving it (thanks to Don):
1) Keep a list of textboxes
Dictionary<string, List<string>> BlocksLabels
{
get
{
if (ViewState["BlockLabels"] == null)
ViewState["BlockLabels"] = new Dictionary<string, List<string>>();
return ViewState["BlockLabels"] as Dictionary<string, List<string>>;
}
set { ViewState["BlockLabels"] = value; }
}
2) In the method that creates the blocks (called from Page_Load):
if (BlocksLabels.ContainsKey(block.ID))
{
foreach (string label in BlocksLabels[block.ID])
block.Controls.Add(new TextBox { ID = labelId });
}
else
{
// Add one empty canvas label by default
string labelId = block.ID + "_label0";
BlocksLabels[block.ID] = new List<string>();
BlocksLabels[block.ID].Add(labelId);
block.Controls.Add(new TextBox { ID = labelId });
}
3) Finally, in the event that adds a new text box
Button senderButton = (Button)sender;
string parentId = senderButton.ID.Replace("_button", "");
Panel targetBlock = (Panel)FindControl(update_panel, parentId);
string labelId = targetBlock.ID + "_label" + BlocksLabels[targetBlock.ID].Count;
BlocksLabels[targetBlock.ID].Add(labelId);
targetBlock.Controls.Add(new TextBox { ID = labelId });