Dynamic Controls

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

hi
I need to create some controls[text box and drop down list] on the fly . How
can i positions these controls in the web form. Also how can i track the
state when i am doing the post back.
Any refrence or examples?

Any help will be appreciated

Thanks
Dave
 
To place the controls you have to manually add then to tables, panels or
placeholders (make sure your control is INSIDE the <form> tag in HTML). You
will find that tables are the best option coz they provide more control over
the position. So, for example, drag a panel in your form and write that
control in page_load:
-------
Table tbl = new Table()
for(int numRows = 0; numRows < 10; numRows ++)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Cells.add(tr);
tbl.rows.add(tr);
HtmlInputText txt = new HtmlInputText();
txt.Name = "txt_" + numRows.ToString();
txt.ID = txt.Name;
tc.Controls.add(txt);
}
Panel1.Controls.add(tbl);
------
Its very important that you set both "name" and "id", coz if you dont, later
you wont be able to recover to value inside the control in the postback.

To recover the value, simply use Request("txt_" + numRows)

HTH
 
Thanks Mariano

Mariano Drago said:
To place the controls you have to manually add then to tables, panels or
placeholders (make sure your control is INSIDE the <form> tag in HTML). You
will find that tables are the best option coz they provide more control over
the position. So, for example, drag a panel in your form and write that
control in page_load:
-------
Table tbl = new Table()
for(int numRows = 0; numRows < 10; numRows ++)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Cells.add(tr);
tbl.rows.add(tr);
HtmlInputText txt = new HtmlInputText();
txt.Name = "txt_" + numRows.ToString();
txt.ID = txt.Name;
tc.Controls.add(txt);
}
Panel1.Controls.add(tbl);
------
Its very important that you set both "name" and "id", coz if you dont, later
you wont be able to recover to value inside the control in the postback.

To recover the value, simply use Request("txt_" + numRows)

HTH


Dave said:
hi
I need to create some controls[text box and drop down list] on the fly . How
can i positions these controls in the web form. Also how can i track the
state when i am doing the post back.
Any refrence or examples?

Any help will be appreciated

Thanks
Dave
 
Back
Top