Ken,
Your code to add a variable number of controls to a page does work.
The part that loops through the controls also works.
However, when I combine your code with my page, it does not work. I
have my controls in a dynamically created table. I need the table,
because I will have several columns of controls. But several things go
wrong. First of all, when I click on Submit, all my textboxes
disappear. The page doesn't maintain them. Secondly, if I try and do
a "for each" loop through the controls, I get no values. (No textboxes
are found in the form). Thirdly, if I try to find a textbox control
like this:
txtbx = place_holder.FindControl("TextBox2")
That also fails. This last method would be useful, because I need
every row of listboxes together, so I can insert them in a row in a
database table.
Anyway, if you are interested in pursuing this, my asp page with all
its code is below, its fairly simple, but it does not work.
BD
<%@Page Language="VB" debug="true" %>
<html>
<head>
<title>Enter Number of Rooms for each day</title>
</head>
<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
dim tRow as TableRow
dim tCell as TableCell
Dim Index as integer
dim NumDays as integer
dim roomTextBox as TextBox
dim Table2 as Table
If Not Page.IsPostBack Then
'''''''''''''''''''''''''''''''
Table2 = new Table
Table2.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1)
tRow = new TableRow()
tCell = new TableCell()
tCell.Text = "<b>Textboxes</b>"
tRow.Cells.Add(tCell)
Table2.Rows.Add(tRow)
'''''''''''''''''''''''''''''''''
NumDays = 3 ' in reality this would be variable
For Index = 0 to NumDays
tRow = new TableRow()
''''''''''''''''''''''''''''
roomTextBox = new TextBox()
roomTextBox.ID = "TextBox" & index
roomTextBox.Text = ""
roomTextBox.Columns = 4
roomTextBox.MaxLength = "4"
roomTextBox.EnableViewState = True
tCell = new TableCell()
tCell.Controls.Add(roomTextBox)
tRow.Cells.Add(tCell)
''''''''''''''''''''''''''''
Table2.Rows.Add(tRow)
Next
PlaceHolder1.Controls.Add(table2)
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''
Sub SubmitClick(objSender As Object, objArgs As EventArgs)
dim NumDays as integer
dim Index as integer
dim ctl as Control
dim strTextBoxContents as String
dim strTextBoxID as string
dim strTest as string
dim place_holder as PlaceHolder
dim frm as HtmlForm
dim txtbx as TextBox
If Page.IsValid() and Page.IsPostBack Then
frm = Page.FindControl("form1")
place_holder = frm.FindControl("placeholder1")
txtbx = place_holder.FindControl("TextBox0")
if txtbx is Nothing then
strTest = "Textbox0 is Nothing " & "<br>"
Else
strTest = "Textbox0.ID is " & txtbx.ID & "<br>"
End If
for each ctl in place_holder.controls
If typeof ctl is textbox then
txtbx = ctype(ctl, TextBox)
strTest = strTest & txtbx.id & "<br>"
end if
next
LabDiagnostic.Text = strTest '!!!
End If
End Sub
</script>
<body bgcolor="#FFFFFF">
<form METHOD="POST" id="form1" runat="server">
<center>
<asp:Label id="LabDiagnostic" runat="server" />
<asp

laceHolder id="PlaceHolder1" runat="server"></asp

laceHolder>
<table border="1">
<tr><td align="center" colspan="2">
<asp:Button id="Button1" runat="server" onclick="SubmitClick"
text="Submit" />
</td></tr>
</table>
</center>
</form>
</body>
</html>
*********************************