Click to See Complete Forum and Search --> : Add TextBox control dynamically !!! (help me now)


h2q1
09-26-2008, 12:50 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!

aniscartujo
10-13-2008, 03:06 PM
Hello,

Textboxes are created on memory; so when you do a postback the textboxes are lost, so the data is not available.

You can save the textboxes on any perdurable variable (ex. an application or session variable) or get the values from the "Request.Form" collection.

Aniscartujo

ewitkows
10-29-2008, 03:41 PM
Use the Page.Controls.Add() method. But as Aniscartujo stated, theyre lost on postback. The answer is to perform your actions within the Page_Load procedure. HTH...

aniscartujo
10-29-2008, 06:50 PM
Under the page load event, iterate trough "Request.Form" collection... you can get "all" the previous page posted items... (any control)

Ex. If you have a textbox on your page named textbox1, you can get the value of that textbox after a postback on the load event using this Request.Form("Textbox1")

Ex2. If you dinamically create a textbox called "ThisISMyTextBox"... and then set the text value to "My Name Is h2q1" and then do a postback, on the next page load event you can get that textbox value using this Request.Form("ThisISMyTextBox")

Aniscartujo

ewitkows
10-29-2008, 06:56 PM
Under the page load event, iterate trough "Request.Form" collection... you can get "all" the previous page posted items... (any control)

Ex. If you have a textbox on your page named textbox1, you can get the value of that textbox after a postback on the load event using this Request.Form("Textbox1")

Ex2. If you dinamically create a textbox called "ThisISMyTextBox"... and then set the text value to "My Name Is h2q1" and then do a postback, on the next page load event you can get that textbox value using this Request.Form("ThisISMyTextBox")

Aniscartujo


That is correct, but request.form relies on the page to be posted to the code-behind page before the controls are available, where page.controls is a true instance of all the objects on the page, and the page.controls.addat() method allows you to specify where on the page you would like youre controls beings added. Which takes me to...

Adding controls via code-behind is (obviously) a messy thing to do! ASP.NET allows you to do this because of how the framework has been built, but adding controls dynamically can be a pain in the rear-end because its difficult to get the location on the page correctly, and to add server side event handling now includes using the AddHandler method. I guess its easy to get spoiled with vis. studio when it allows you to "drag and drop" stuff ;)