Dang Quang

Dang Quang

  • NA
  • 1
  • 0

Add TextBox control dynamically !!! (help me now)

Sep 26 2008 2:02 PM
Hi...
I'm having some trouble with TextBox (dynamically).
 My application have a textbox to enter number of answer (txtNum), a button (btnAdd) to show textboxes for enter answer (with number gived), a button(btnSubmit) to get value of textboxes that entered answer.

I decided put textboxes (dynamically) in PlaceHolder with
 <asp:PlaceHolder runat="server" id="TextBoxesHere" />

Follow is code of btnAdd:

protected void btnAdd_Click(object sender, EventArgs e)
{
  int numAns= Convert.ToInt32(txtNum.Text.Trim());

            for (int i = 1; i <= numAns; i++)
            {
                TextBoxesHere.Controls.Add(new TextBox());
            }      
}



and code of btnSubmit:

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            String[] arr = new String[10];
            int count=0;

            foreach (Control c in TextBoxesHere.Controls)
            {                           
                if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") && c.ID == null)
            //c.ID = null means that textboxes were not entered
                {               
                    arr[count] = ((TextBox)c).Text;
                    Response.Write(arr[count].ToString());
                    count++;
                }
            }
 
        }


After I entered 3 for number of answer, web page show 3 textboxes. Then, i entered data for them but when I click SUbmit button, web page didn't display anything...

Wha'ts wrong???? :(
Thanks for read and hope explained soon!

Answers (1)