0

I need to iterate trough all dynamically generated text boxes on page when clicking on "Download" button. Following code do not return anything:

    protected void Download_Click(object sender, EventArgs e)
    { //???
        foreach (Control child in this.form1.Controls)
        {
            if (child.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")) //child is somehow never textbox
            {
                TextBox textBox = (TextBox)child;
                System.Diagnostics.Debug.WriteLine(textBox.Text);
            }
        }
    }

I create textBoxes by calling AddTextbox function:

    private void AddTextbox(string id, bool isNumber)
    {
        TextBox textBox = new TextBox();
        textBox.ID = id; //set ID
        if (isNumber){ textBox.Attributes.Add("type", "number"); }; //if only numbers can be added, html attribute 'type = "number"' is added
        form1.Controls.Add(textBox);
        form1.Controls.Add(new LiteralControl("<br />")); //just add <br /> html element            
    }
6
  • Do you recreate the dynamically created textboxes on form init? In addition, you need to iterate the children of the controls if the TextBoxes are not located directly on the form. Commented Feb 19, 2018 at 8:49
  • No, I guess I'm not. I create them with one function, then iterate them with second to fill values and then I'm trying to download them as showed in this question. Can I ask you for some reference or tutorial based on which I will be able to do that? >second question: Yes, you are right, I need to fix that also. Commented Feb 19, 2018 at 8:56
  • beside the diagnostic, do you debug it ? Commented Feb 19, 2018 at 8:58
  • yes. child is never textbox, breakpoint inside of that if statement won't fire even once. Commented Feb 19, 2018 at 9:02
  • Actually I am not clear to what you want to achieve. :( Commented Feb 19, 2018 at 9:07

3 Answers 3

1

Here is a full working sample of what you want to achieve:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>

    <form id="form1" runat="server">
    <div>
<asp:Panel ID="pnlTextBoxes" runat="server">
</asp:Panel>
    <asp:Button ID="btnAdd" runat="server" Text="Add New" OnClick="AddTextBox" />
    <asp:Button ID="btnGet" runat="server" Text="Download" OnClick="Download_Click" />

    </div>
    </form>
</body>
</html>

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
            int i = 1;
            foreach (string key in keys)
            {
                this.CreateTextBox("txtDynamic" + i);
                i++;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void AddTextBox(object sender, EventArgs e)
        {
            int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
            this.CreateTextBox("txtDynamic" + index);
        }

        private void CreateTextBox(string id)
        {
            TextBox txt = new TextBox();
            txt.ID = id;
            pnlTextBoxes.Controls.Add(txt);

            Literal lt = new Literal();
            lt.Text = "<br />";
            pnlTextBoxes.Controls.Add(lt);
        }

        protected void Download_Click(object sender, EventArgs e)
        { //???
            foreach (Control child in pnlTextBoxes.Controls)
            {
                if (child.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox")) //child is somehow never textbox
                {
                    TextBox textBox = (TextBox)child;
                    System.Diagnostics.Debug.WriteLine(textBox.Text);
                }
            }
        }

    }
}

The most important part is the Page_PreInit, where you have to recreate the dynamically created TextBoxes. If something is not clear to you, please let me know (The code is inspired from this page https://www.aspsnippets.com/Articles/Get-Value-Text-of-dynamically-created-TextBox-in-ASPNet-using-C-and-VBNet.aspx )

Sign up to request clarification or add additional context in comments.

Comments

1

Did you try this.Form.Controls instead of this.Controls? this refers to the .aspx Page, the TextBox normally is not a direct child of the page, it should be a child of the Form.

4 Comments

Yes, do not work. It seems like my problem is not creating textBoxes properly.
Can you post the code used to create the TextBoxes?
I think that your code doesn't persist the TextBoxes after the PostBack
You need to re-create your TextBoxes on each PostBack, this link explains how to do it very well: aspsnippets.com/Articles/…
1

The issue you are having will be that although you are creating the textboxes to be displayed on the page, when the download button click method is called, they no longer exist.

When the page is initially loaded, you are creating the textboxes and the html is rendered including them.

When the download button is clicked, a postback occurs. What this actually does is call a new instance of the page. So the textboxes have not been created at this point.

So before you can access the textboxes under form1.Controls, you will need to recreate them first using the same method that created them in the first place. However, they will not have the value that may have been input via the browser.

This data should be in the posted form though, so you would need to modify your AddTextBox method to attempt to get the data from the post data. Something like:

private void AddTextbox(string id, bool isNumber)
{
    TextBox textBox = new TextBox();
    textBox.ID = id; //set ID
    if (isNumber){ textBox.Attributes.Add("type", "number"); }; //if only numbers can be added, html attribute 'type = "number"' is added
    if (Page.IsPostBack)
    {
        // set value from passed form data
        textbox.Text = Request.Form[id];
    }
    form1.Controls.Add(textBox);
    form1.Controls.Add(new LiteralControl("<br />")); //just add <br /> html element            
}

I'm not sure, but you may also need to set textbox.ClientIdMode = static so that the form's html is rendered with the exact IDs you specify.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.