I am trying to create Text Box controls dynamically at the user's request.
The problem is that I can't, in the same postback, both create the dynamic textbox and retrieve the number that the user entered, and use that to create the other text boxes.
It seems that I'm always one postback behind.
That's the entire code in my example and I have no other code in the HTML page.
Please help my fix the problem so i could create the number of textboxes the user requested at the moment he requested them.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
form1.InnerText = "how many TextBoxes would you like to add?";
form1.InnerHtml += "<br />";
TextBox tb = new TextBox();
tb.ID = "TextBox1";
tb.Attributes.Add("type", "number");
tb.Attributes.Add("runat", "server");
tb.Attributes.Add("min", "1");
tb.Attributes.Add("max", "5");
tb.AutoPostBack = true;
tb.TextChanged += new EventHandler(textBox_TextChanged);
form1.Controls.Add(tb);
if (tb.Text != "")
{
for (int i = 0; i < Convert.ToInt16(tb.Text); i++)
{
TextBox tb1 = new TextBox();
tb1.ID = "newTB" + i.ToString();
form1.Controls.Add(tb1);
}
}
}
protected void textBox_TextChanged(object sender, EventArgs e)
{
}
}
I tried writing the shortest and simplest of codes as i'm only interested in figuring out the principle that stands behind my problem, and I couldn't find a question similar enough to mine.
Ok so your code isn't following standard asp.net coding practises. The following is a simple working example of what you are trying to achieve:
Form Code
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label">How many textboxes would you like to create?</asp:Label>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Invalid Number" MaximumValue="5"
MinimumValue="1"></asp:RangeValidator>
</div>
</form>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
for (int i = 0; i < Convert.ToInt16(TextBox1.Text); i++)
{
TextBox tb1 = new TextBox();
tb1.ID = "newTB" + i.ToString();
form1.Controls.Add(tb1);
}
}
}
I'd really recommend reading up on the asp.net page lifecycle and working through some basic examples to understand the fundamental principles. This tutorial is a good place to start.
Edit - Revised: Below is another stab at your question, again needs a bit love and refactoring (and I'm sure there may be better ways of doing this!) but it works. In my test form I had a submit button and it the created textboxes then 'retained' their value between postbacks (but they are actually being recreated from the Request).
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
foreach (string key in Request.Form)
{
if (key.StartsWith("newTB"))
{
TextBox tb1 = new TextBox();
tb1.ID = key;
tb1.Text = Request.Form[key];
form1.Controls.Add(tb1);
}
}
}
}
protected void Page_Init(object sender, EventArgs e)
{
Label label1 = new Label()
{
ID = "label1",
Text = "How many textboxes would you like to create?"
};
form1.Controls.Add(label1);
TextBox textBox1 = new TextBox()
{
ID = "TextBox1",
AutoPostBack = true,
CausesValidation = true
};
textBox1.TextChanged += new EventHandler(TextBox1_TextChanged);
form1.Controls.Add(textBox1);
RangeValidator rangeValidator = new RangeValidator()
{
ID="RangeValidator1",
ControlToValidate = "TextBox1",
MaximumValue = "5",
ErrorMessage = "Out of range!",
MinimumValue = "1"
};
form1.Controls.Add(rangeValidator);
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox textBox = form1.FindControl("TextBox1") as TextBox;
if (Page.IsValid && textBox.Text != "")
{
RemoveOldControls();
for (int i = 0; i < Convert.ToInt16(textBox.Text); i++)
{
TextBox tb1 = new TextBox();
tb1.ID = "newTB" + i.ToString();
form1.Controls.Add(tb1);
}
}
}
//Recursive method to remove all "newTB" textboxes
private void RemoveOldControls()
{
for (int i = 0; i < form1.Controls.Count; i++)
{
if (!string.IsNullOrEmpty(form1.Controls[i].ID) && form1.Controls[i].ID.StartsWith("newTB"))
{
TextBox newTextBox = form1.Controls[i] as TextBox;
if (newTextBox != null)
form1.Controls.Remove(newTextBox);
RemoveOldControls();
break;
}
}
}