How to loop through dynamically added controls after a postback ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • myth0s
    New Member
    • Jan 2008
    • 32

    How to loop through dynamically added controls after a postback ?

    Hello,

    I have a .aspx page (VB.NET) where there is the following table :

    Code:
    <asp:Table ID="mainTable" CssClass="tableList" runat="server">
            <asp:TableHeaderRow CssClass="tableHeader">
                <asp:TableHeaderCell>Type</asp:TableHeaderCell>
                <asp:TableHeaderCell>Task</asp:TableHeaderCell>
                <asp:TableHeaderCell>Length</asp:TableHeaderCell>
                <asp:TableHeaderCell>Done</asp:TableHeaderCell>
            </asp:TableHeaderRow>
    </asp:Table>
    In the .vb file, I connect to a database and add some rows

    Code:
    Dim mainTableRow As TableRow = New TableRow()
    ...
    Dim mainTableRowCell4 As TableCell = New TableCell()
    Dim checkBoxTemp As CheckBox = New CheckBox()
    checkBoxTemp.ID = "ent_" & dataReader.GetInt32(2).ToString
    mainTableRowCell4.Controls.Add(checkBoxTemp)
    mainTableRow.Cells.Add(mainTableRowCell4)
    
    mainTable.Rows.Add(mainTableRow)
    This runs fine and add some rows to the table. Every row has the checkbox control and the id and name attributes of the checkbox are "ent_##".

    My problem is : when I click on the save button, which is nothing more then a simple asp:button in the "form1" form, I have trouble finding all my checkboxes.

    If I use something as :
    Code:
    Dim controlesEnum As IEnumerator
    controlesEnum = mainTable.Controls.GetEnumerator()
    I will be able to access the tableheaderrow (because it's static) but not a single other row (because they are added dynamically I guess?).

    I cannot predict how many rows (and therefore checkboxes) I will add to the table. Is there a property or a method I could use to simply loop through all the submited controls, whether they are static or not ??

    Thank you,
    - Alexandre
  • myth0s
    New Member
    • Jan 2008
    • 32

    #2
    Nevermind folks, I now loop request params (Request.Params .GetEnumerator( )) and check for the checkboxes names with a regular expression :)

    Comment

    Working...