by SkRSYXZlb » Wed, 20 Dec 2006 15:17:01
've read a lot of posts on this issue, and was pretty sure all I needed to
do was cycle through the controls collection of the container, but I can't
seem to figure out where the controls are contained when I am adding them to
table cells (of table rows of an ASP.NET TABLE control).
Basically, I have a web form that contains some fields, and then a
dropdownlist where the user says how many rows of input they need (up to a
max of 15). On the dropdownlist's _SelectedIndexChanged event, I make the
table visible and only add as many rows as I need. Each row contains 6
columns, and each column contains 1 control - either a textbox or a
dropdownlist.
The code below works perfectly as far as displaying the input table with
desired controls, and all the controls seem to work as expected. The problem
is that I can not seem to be able to retrieve the values later (when the
submit button is pushed). The table's control collection is empty, and the
table's rows collection always seems empty (and rows count is zero) when
debugging at run time even though I can see the table populated with the
correct number of rows and I can populate the textboxes and dropdownlists as
expected in the browser before I submit.
I was expecting the programmatically defined controls to be members of the
controls collection for the table itself, the row's controls collection,
and/or the row's cells' collection, but as mentioned above the rows count
always appears as zero when I run this, and can not find the visible controls
contained anywhere.
The initial table is simply defined on the form as:
<p>
<asp:Table ID="tblResources" runat="server" BorderColor="Black"
BorderStyle="Solid" BorderWidth="1px" GridLines="Both" Visible="False"
style="line-height: normal">
</asp:Table>
</p>
It is initially hidden, and then when the user selects the number of rows to
display, it made visible and populated with rows, cells and controls using
the following code:
Protected Sub ddlNbrResources_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ddlNbrResources.SelectedIndexChanged
Dim rowCnt As Integer
Dim rowCtr As Integer
Dim cellCnt As Integer
Dim cellCtr As Integer
tblResources.Visible = True
rowCnt = CInt(ddlNbrResources.SelectedValue) 'User specified # of
rows
cellCnt = 6 'Fixed # of table columns
For rowCtr = 1 To rowCnt
Dim tRow As New TableRow()
For cellCtr = 1 To cellCnt
Dim tCell As New TableCell()
Select Case cellCtr
Case 1
Dim tbResName As TextBox = New TextBox()
tbResName.ID = "tbResName" & CStr(rowCtr)
tbResName.Width = 140
tCell.Controls.Add(tbResName)
Case 2
Dim tbResID As TextBox = New TextBox()
tbResID.ID = "tbResID" & CStr(rowCtr)
tbResID.Width = 50
tbResID.MaxLength = 6
tCell.Controls.Add(tbResID)
Case 3
Dim ddlResCountry As DropDownList = New
DropDownList()
ddlResCountry.ID = "ddlResCountry" & CStr(rowCtr)