Retrieving Values from Dynamically Created Controls

Retrieving Values from Dynamically Created Controls

Post 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)
 
 
 

Retrieving Values from Dynamically Created Controls

Post by wawan » Wed, 20 Dec 2006 17:06:25

i,

Based on my understanding, you're trying to create dynamic count of table
rows which have predefined server controls; and you want to obtain the
values user have input in them. However, you're seeing that the controls
doesn't get recreated upon postback.

This is the expected behavior, remember ASP.NET is stateless, the control
hierarchy needs to be re-created upon every postback at server-side. For
controls added dynamically, you need a way to re-create them at server-side
again. For more information, please refer to following KB article:

#HOW TO: Dynamically Create Controls in ASP.NET with Visual Basic .NET
http://support.microsoft.com/kb/317515



However, for your specific requirement, this is not the best way to do this
by doing this yourself. You can use the Repeater control to create those
dynamic controls for you, and use it to read the values after that:

<asp:Repeater ID="repeater1" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:TextBox ID="tbResName"
runat="server"></asp:TextBox></td>
<td>
<asp:TextBox ID="tbResID"
runat="server"></asp:TextBox></td>
<td>
<asp:DropDownList ID="ddlResCountry"
runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Asia /
Pacific</asp:ListItem>
<asp:ListItem>Canada</asp:ListItem>
<asp:ListItem>EMEA</asp:ListItem>
<asp:ListItem>Latin
America</asp:ListItem>
<asp:ListItem>United
States</asp:ListItem>
</asp:DropDownList>
</td>
<td><asp:TextBox ID="tbResFrom"
runat="server"></asp:TextBox></td>
<td><asp:TextBox ID="tbResTo"
runat="server"></asp:TextBox></td>
<td>
<asp:DropDownList ID="ddlResAppOrg"
runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Apps</asp:ListItem>
<asp:ListItem>BPO</asp:ListItem>
<asp:ListItem>ITO</asp:ListItem>
<asp:ListItem>EA</asp:ListItem>
<asp:ListItem>SA</asp:ListItem>
<asp:ListItem>BD</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTe
 
 
 

Retrieving Values from Dynamically Created Controls

Post by SkRSYXZlb » Wed, 20 Dec 2006 17:23:00

alter - thank you very much for the quick response, and for giving me the
option of pursuing my original way via the link, and the better way using the
Repeater control. After a cursory review of the KB article I definitely want
to pursue use of the repeater.

Again, thanks, and have a great day!

"Walter Wang [MSFT]" wrote: