Hi Black (it would be nicer to address a real name),
I made some changes to your code that I think is close to what you're after.
Notice that I give the table an ID so it is easier to find and I loop
through all of the rows of the table and cells within the rows and controls
within the cells looking for a textbox.
Let us know if this helps?
Ken
Microsoft MVP [ASP.NET]
<%@ page debug="true" trace="true" language="VB" %>
<html>
<head>
<title>Enter Number of Rooms for each day</title>
</head>
<script runat="server" language="VB">
Sub Page_Load(ByVal Source As Object, ByVal 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
'''''''''''''''''''''''''''''''
Table2 = New Table
Table2.ID = "Table2"
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 Sub
'''''''''''''''''''''''''''''''''''''''''''''''
Sub SubmitClick(ByVal objSender As Object, ByVal 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
Dim tbl2 As Table
tbl2 = place_holder.FindControl("Table2")
If Not IsNothing(tbl2) Then
For Each rw As TableRow In tbl2.Rows
For Each cll As TableCell In rw.Cells
For Each obj As Object In cll.Controls
If TypeOf obj Is TextBox Then
txtbx = CType(obj, TextBox)
strTest = strTest & txtbx.ID & "=" &
txtbx.Text & "<br>"
End If
Next
Next
Next
End If
LabDiagnostic.Text = strTest '!!!
End If
End Sub
</script>
<body bgcolor="#FFFFFF">
<form id="form1" runat="server" method="POST">
<center>
<asp:label id="LabDiagnostic" runat="server"></asp:label>
<asp:placeholder id="PlaceHolder1"
runat="server"></asp:placeholder>
<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>
<BL*********@LYCOS.COM> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
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:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>
<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>
*********************************