this is my code::

TextBox[] tbArray = new TextBox[] { TextBox1, TextBox1, TextBox1 };


                for (int j = 0; j < tbArray.Length ; j++)
                {
                  
                        tbArray[j].Visible = true;
                     
                }

this code is success but only one textbox show not three textbox.. Can any one help me. Thanks

Dani AI

Generated

Short diagnosis: — you only saw one textbox because you were reusing the same server-control instance three times. Putting the same control reference into an array doesn't create new controls; it just makes multiple references to the single TextBox that already exists on the page.

Two clean fixes: 1) If you declared three TextBoxes in markup, reference each distinct control (what suggested). 2) If you need them created at runtime, instantiate new TextBox objects, give each a unique ID, and add them to a container. Important lifecycle tip: dynamic controls must be recreated on every request and created early (Page_Init or PreInit) so ViewState and posted values are restored. 's runtime approach is on the right track, but creating controls only inside a first-load (!IsPostBack) block will lose values on postback.

Example pattern (recreate controls in Init, keep a count in ViewState, read values on postback):

protected void Page_Init(object sender, EventArgs e)
{
    int count = ViewState["fieldCount"] as int? ?? 3;
    for (int i = 0; i < count; i++)
    {
        var tb = new TextBox { ID = "dynTxt_" + i };
        phFields.Controls.Add(tb);
        phFields.Controls.Add(new LiteralControl("<br/>"));
    }
}

protected void AddField_Click(object sender, EventArgs e)
{
    ViewState["fieldCount"] = ((int?)ViewState["fieldCount"] ?? 3) + 1;
    // controls will be rebuilt on next lifecycle (Init)
}

protected void Save_Click(object sender, EventArgs e)
{
    int count = (int?)ViewState["fieldCount"] ?? 3;
    for (int i = 0; i < count; i++)
    {
        var tb = phFields.FindControl("dynTxt_" + i) as TextBox;
        var text = tb?.Text ?? string.Empty;
        // process text
    }
}

Troubleshooting checklist: always use unique IDs, recreate dynamic controls on every request before LoadViewState, keep a stable count/key in ViewState (or a hidden field), and use a PlaceHolder (or Panel) to hold dynamic children. These steps reliably preserve values and avoid the “only one textbox” symptom.

Recommended Answers

All 2 Replies

Check this in Page_Load event

if (!IsPostBack)
        {
            for (int i = 0; i < 3; i++)
            {
                TextBox txt = new TextBox();
                txt.ID = "txt" + i.ToString(); //or "txt" + pnlview.Controls.Count.ToString();
                pnlview.Wrap = true;
                pnlview.Controls.Add(txt);
                pnlview.ScrollBars = ScrollBars.Both;
            }
        }

#
write this
TextBox[] tbArray = new TextBox[] { TextBox1, TextBox2, TextBox3 };
#
#
instead of
TextBox[] tbArray = new TextBox[] { TextBox1, TextBox1, TextBox1 };
#

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.