Hello all,

Need your assistance on this, I've got a page that I'm creating textboxes dynamically x many whenever the user enters a value in a textbox

i.e
Item A was delivered and it came in 8 pallets, the user will press a button and will create 8 textboxes.

The textboxes created are placed on a panel. All of the dynamic creation works fine aside from the fact that I'm struggling to get the content of dynamically created textboxes, whenever the user presses the submit button.

I've approached several ways of this but none seems to be working:

Approach 1:

On submit, it calls a function called getTBValues

Code:
    void getTBValues()
    {
        int iCntr = 0;

        Int32.TryParse(txtPalletNo01.Text, out iCntr);

        for (int i = 0; i < iCntr; i++)
        {
            string strId = i.ToString("0000");

            TextBox tb = (TextBox)pnl01.FindControl(strId);  //pnl01 is the id of my panel

            string txt = tb.Text;
        }
    }
However the above code returns null! Not sure as to why hence the resulting query to get the text will result in an error

Approach 2:

Created a "hack" to add an onblur functionality to the dynamically created text box through the onclick button event.

Code:
//an extract only
protected override void  OnInit(EventArgs e)
    {
        _newTextBox = new TextBox[1000];

        for (int i = 0; i < 1000; i++)
        {
            _newTextBox[i] = new TextBox();
            _newTextBox[i].ID = "tb" + i.ToString("0000");
            _newTextBox[i].Width = 80;
        }

        InitializeComponent();
        base.OnInit(e);
    }

    private void InitializeComponent()
    {
        this.btnBlur01.Click += new EventHandler(btnBlur01_Click);
    }

    private void btnBlur01_Click(object sender, EventArgs e)
    {
        TextBox tbSender = (TextBox)sender;
        string strId = tbSender.ID;

        string strLblTotal = LabelTotal01.Text;

        if (strLblTotal.Length > 0)
            strLblTotal += "~" + strId + ":" + tbSender.Text;
        else
            strLblTotal = strId + ":" + tbSender.Text;

        LabelTotal01.Text = strLblTotal;
    }

    protected void btn01_Click(object sender, EventArgs e)
    {
        pnl01.Visible = true;
        lbl01.Visible = true;
        lbl01.Text = "Qty per pallet: ";

        string strIn = txtPalletNo01.Text;

        int iCntr = 0;

        Int32.TryParse(strIn, out iCntr);

        if (iCntr > 0)
        {
            try
            {
                for (int i = 0; i < iCntr; i++)
                {
                    string strEvntHandler = ClientScript.GetPostBackEventReference(this.btnBlur01, "");
                    _newTextBox[i].Attributes.Add("onblur", strEvntHandler);

                    pnl01.Controls.Add(_newTextBox[i]);
                    pnl01.Controls.Add(new LiteralControl("<br />"));
                }
            }
            catch { }
        }
    }
Again I was unsuccessful on this.



I'm now thinking of creating a prerender event attached to the textbox, for the onblur event and then somehow use a javascript to populate an asp label with the values for each of the dynamically created textbox. I can then also use this to validate user input?

Any ideas on how I should move forward or correct any functionality I have mentioned above.

Hope this all make sense.


Whatever input you have on this is trully appreciated.

Greyskull