Question

I have an .aspx page and I want to dynamically add textboxes to the page on the click of a button. For this purpose I've added a placeholder on the page and add the controls to the server side when the button is clicked.

<asp:PlaceHolder runat="server" ID="NotificationArea"></asp:PlaceHolder>
<asp:Button ID="AddNotification" runat="server" Text="Add" OnClick="AddNotification_Click" />
<asp:Button ID="RemoveNotification" runat="server" Text="Remove" OnClick="RemoveNotification_Click" />

I store the textboxes in a session variable so that I can continue adding and removing textboxes indefinitely. Below I put the on_click methods for the add and remove buttons:

protected void AddNotification_Click(object sender, EventArgs e)
{
    List<TextBox> notifications = (List<TextBox>)(Session["Notifications"]);
    notifications.Add(new TextBox());
    notifications[notifications.Count - 1].Width = 450;
    notifications[notifications.Count - 1].ID = "txtNotification" + notifications.Count;

    foreach (TextBox textBox in notifications)
    {
        NotificationArea.Controls.Add(textBox);
    }

    NotificationArea.Controls.Add(notifications[notifications.Count - 1]);
    Session["Notifications"] = notifications;
}

protected void RemoveNotification_Click(object sender, EventArgs e)
{
    List<TextBox> notifications = (List<TextBox>)(Session["Notifications"]);
    if (notifications.Count > 0)
    {
        NotificationArea.Controls.Remove(notifications[notifications.Count - 1]);
        notifications.RemoveAt(notifications.Count - 1);
    }

    foreach (TextBox textBox in notifications)
    {
        NotificationArea.Controls.Add(textBox);
    }

    Session["Notifications"] = notifications;
}

This works nicely. It continually adds new textboxes and removes the last one if the remove button is clicked. Then, when I tried to get the text from the textboxes I ran into a problem. I never actually store the text that are typed into the textboces in the session variable. Just the empty textboxes that are initially created. Also, see below:

int count = NotificationArea.Controls.Count;

Debugging this shows that the count of the controls in the NotificationArea is 0. How do I access the text for these dynamically added textbox controls? Do I somehow add an ontext_change event to the textboxes that saves the particular textbox's Text into its equivalent in the session variable? How do I go about doing that?

Was it helpful?

Solution

Found the solution here. Turns out you need to re-create all controls that were added dynamically on each post back

public void Page_Init(object sender, EventArgs e)
{
    CreateDynamicControls();
}

private void CreateDynamicControls()
{
    notifications = (List<TextBox>)(Session["Notifications"]);

    if (notifications != null)
    {
        foreach (TextBox textBox in notifications)
        {
            NotificationArea.Controls.Add(textBox);
        }
    }
}

Doing this allows me to access the content of these controls at any time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top