I have a web application in which I won't know the number of fields I need until runtime. Say I'm reading a form structure from a database which will tell me all of the fields i need to generate. I then loop through that collection and add a label and a text box. It may be 3 or 30 controls I need to add. I'm looking the page you sent, but I'm not sure how it applies.
The controls are correctly generated for the user, but on post back they simply don't exist. I tried looping through the controls in my panel, but it shows zero on postback. I tried looping through the entire page, but couldn't find any TextBox controls.
I read somewhere that seemed to imply that I need to recreate the controls on postback and ASP.NET would take care of settings values that the user entered.
I tried this, but the values don't get set.
If I run this code on postback, there are no Textbox on the page
private void LoopThroughControls(Contro
l control)
{
foreach (Control frmCtrl in control.Controls)
{
Response.Write(frmCtrl.Get
Type().ToS
tring());
Response.Write("<br />");
if (frmCtrl.GetType().ToStrin
g() == "System.Web.UI.WebControls
.TextBox")
RunTimeParams.Add(frmCtrl.
ID, "Test");
if (frmCtrl.HasControls() == true)
LoopThroughControls(frmCtr
l);
}
}
Here's the code I'm using to create the controls.
foreach(...)
{
Label FieldLabel = new Label();
FieldLabel.Text = FieldFromDB;
TextBox FieldTextBox = new TextBox();
RuntimeParamTextBox.ID = "DBField" + i;
TableRow InputTableRow = new TableRow();
TableCell InputTableCellLabel = new TableCell();
ParamInputTableCellLabel.C
ontrols.Ad
d(RuntimeP
aramLabel)
;
TableCell ParamInputTableCellText = new TableCell();
ParamInputTableCellText.Co
ntrols.Add
(RuntimePa
ramTextBox
);
ParamInputTableRow.Control
s.Add(Para
mInputTabl
eCellLabel
);
ParamInputTableRow.Control
s.Add(Para
mInputTabl
eCellText)
;
ParamInputTable.Controls.A
dd(ParamIn
putTableRo
w);
}
RunTimeParamsPanel.Control
s.Add(Para
mInputTabl
e);